Common

Convert Dp to Pixel

This function converts a value in density-independent pixels (dp) to pixels (px).

fun convertDpToPixel(dp: Float, context: Context): Float {
    val resources = context.resources
    val metrics = resources.displayMetrics
    return dp * (metrics.densityDpi.toFloat() / 160f)
}
  • Parameters: dp (the value in dp), context (used to get resources and display metrics).

  • Process: Uses the display metrics to convert dp to pixels.

  • Return: The equivalent value in pixels.

Get Bitmap from Drawable

This function converts a Drawable to a Bitmap.

fun getBitmapFromDrawable(drawable: Drawable): Bitmap {
    val bmp = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bmp)
    drawable.setBounds(0, 0, canvas.width, canvas.height)
    drawable.draw(canvas)
    return bmp
}
  • Parameters: drawable (the drawable to convert).

  • Process: Creates a bitmap, draws the drawable onto a canvas, and returns the bitmap.

  • Return: A Bitmap representation of the drawable.

Get Colored String

This function creates a SpannableString with a specified color.

fun getColoredString(text: String, color: Int): SpannableString {
    val word = SpannableString(text)
    word.setSpan(ForegroundColorSpan(color), 0, word.length, 33)
    return word
}
  • Parameters: text (the string to color), color (the color to apply).

  • Process: Creates a SpannableString and applies a ForegroundColorSpan.

  • Return: A colored SpannableString.

Get Current Date

This function returns the current date in a specified format and locale.

fun getCurrentDate(format: String, locale: Locale): String {
    return SimpleDateFormat(format, locale).format(Date())
}
  • Parameters: format (date format), locale (locale for the format).

  • Process: Uses SimpleDateFormat to format the current date.

  • Return: A string representing the current date in the specified format and locale.

Get Device ID

This function returns the device's unique ID.

@SuppressLint("HardwareIds")
fun getDeviceId(context: Context): String =
    Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
  • Parameters: context (used to access system settings).

  • Process: Retrieves the device's unique ID from the system settings.

  • Return: The device's unique ID as a string.

Get Screen Width

This function returns the screen width in pixels.

fun getScreenWidth(): Int {
    return Resources.getSystem().displayMetrics.widthPixels
}
  • Parameters: None.

  • Process: Uses system resources to get the screen width.

  • Return: The screen width in pixels.

Hide Soft Keyboard

This function hides the soft keyboard.

fun hideSoftKeyboard(view: View, context: Context?) {
    val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    imm?.hideSoftInputFromWindow(view.windowToken, 0)
}
  • Parameters: view (the view currently holding the keyboard focus), context (used to get the input method manager).

  • Process: Hides the soft keyboard using the input method manager.

  • Return: None.

is Network Connected

This function checks if the device is connected to a network.

fun isNetworkConnected(): Boolean {
    val connectivityManager = ContextProvider.get().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val network = connectivityManager.activeNetwork ?: return false
    val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
    return when {
        activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
        activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
        activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
        else -> false
    }
}
  • Parameters: None.

  • Process: Uses ConnectivityManager to check network connection and capabilities.

  • Return: true if connected, false otherwise.

Load Json from Asset

This function loads a JSON file from the assets folder.

fun loadJSONFromAsset(context: Context, jsonFileName: String): String {
    val manager = context.assets
    val inputStream = manager.open(jsonFileName)
    val buffer = ByteArray(inputStream.available())
    inputStream.read(buffer)
    inputStream.close()
    val charset = Charset.forName("UTF-8")
    return String(buffer, charset)
}
  • Parameters: context (to access assets), jsonFileName (the name of the JSON file).

  • Process: Reads the JSON file from assets and returns its content as a string.

  • Return: The JSON content as a string.

Show Keyboard

This function shows the soft keyboard.

fun showKeyboard(view: View, context: Context?) {
    if (view.requestFocus()) {
        val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }
}
  • Parameters: view (the view to focus), context (to access the input method manager).

  • Process: Requests focus for the view and shows the soft keyboard using the input method manager.

  • Return: None.

Last updated