Skip to content

Extensions

Here we will learn more about extension function that we can use in this codebase.

Date

For formatting date purposes, we can parse String to Date by calling toDate() function like this:

val resultDate = stringDate.toDate(fromFormat = "yyyy-MMM-dd", locale = Locale("id","ID"))
as you can see, it takes two parameters:

  • fromFormat the format Date of the string you want to parse
  • locale the locale of the String, it has default value of Locale.getDefault() so you can leave it empty

The other way around, we can parse Date to String too by calling toString() function like this:

val resultString = theDate.toString(toFormat ="dd MMMM yyyy", locale  = Locale("id","ID"))
and the parameters are:

  • toFormat the string format you want to parse
  • locale the locale of the String, it has default value of Locale.getDefault() so you can leave it empty

String

Currency

When formatting number string into currency, use this method:

val numberString = "1000000"
// By default, the Locale is Locale.getDefault()
val formattedString = numberString.toCurrency(Locale.US) // (1) 
  1. This will return "US 1,000,000"

ImageView

For loading image to imageView, you can call loadImage() function. But first, let's discuss each parameters:

  • For the image source, you can choose one out of three options
  • imageDrawable when you want to load drawable to ImageView
  • imageUrl when you want to load online image to ImageView
  • imageRes when you want to load resourceId to ImageView
  • For placeholder while loading image, you can choose one out of two options
  • placeHolderDrawable when the placeholder is a drawable
  • placeHolderResource when the placeholder using resource id
  • progressBar the progressbar to load if needed, leave it empty if not needed
  • For when error while loading image, you can display error image and you can choose one out of two options
  • errorDrawable when the error image using drawable
  • errorResourceId when the error image using resource id
  • scaleType for defining scale type of the image, it has default value of CENTER_CROP

huffthh... that was quite a lot, but then you can load and image just like this:

binding.ivUser.loadImage(
        c = context,
        imageUrl = user.avatar,
        placeHolderResourceId = R.drawable.ic_guest,
        errorDrawable = getDrawableResource(R.drawable.ic_guest)
)

Measure

Ever wonder how to convert your Int to measure that you can use for your View, worry now that now you can use this function:

// For converting Int to Dp
val desiredDP = 15.toDp()

// For converting Int to Px
val desiredPx = 76.toPx()

Nullability

For check does your object, variable, or even class is null or not.. now you can do that like this:

val doesClassNull = any.isnull()

// On the other hand, you can do something like this too
val doesClassNotNull = any.isNotNull()

View

For global View, we can call these functions for multiple purposes:

// display View
theView.visible()

// hide View
theView.invisible()

// remove View
theView.gone()

// enable View
theView.enable()

// disable View
theView.disable()