While working on android apps we use a number of utility classes to encapsulate certain logic that we need on multiple places in the application. We use this android utility classes to display or hide UI elements, to check the network connection etc. We are using Kotlin in this example, here are some of this utility classes that we use often:
NetworkUtility
We use this utility class whenever we need to check in the app if there is internet available or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
object NetworkUtil { @Suppress("DEPRECATION") fun isInternetAvailable(context: Context): Boolean { var result = false val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { cm?.run { cm.getNetworkCapabilities(cm.activeNetwork)?.run { result = when { hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true else -> false } } } } else { cm?.run { cm.activeNetworkInfo?.run { if (type == ConnectivityManager.TYPE_WIFI) { result = true } else if (type == ConnectivityManager.TYPE_MOBILE) { result = true } } } } return result } } |
You can use this class to check if the internet is available like this:
1 |
NetworkUtil.isInternetAvailable(context) |
 InputValidatorUtility
We use this utility class to check if a mandatory input field is empty or not:
1 2 3 4 5 6 |
object InputValidator { fun isRequiredFieldNotEmpty(inputField: String): Boolean { return !TextUtils.isEmpty(inputField) } } |
You can use this class to check an input field like this:
1 2 3 |
if (isRequiredFieldNotEmpty(username) && isRequiredFieldNotEmpty(password)) { //do something } |
InputViewHandler
We use this utility class to hide the keyboard and to show the cursor on edit text elements on touch events:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
object InputViewHandler { fun hideKeyboard(view: View) { val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(view.windowToken, 0) } fun showCursor(view: View) { if (view is EditText){ view.isCursorVisible = true } } } |
You can use it like this:
1 2 |
InputViewHandler.showCursor(view) InputViewHandler.hideKeyboard(view) |
MessageUtility
We use this class to show different kinds of messages to the user. We can show toast messages, snackbar, snackbar with action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
object MessageUtil { fun Context.shortToast(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show() fun Context.longToast(message: String) = Toast.makeText(this, message, Toast.LENGTH_LONG).show() fun Activity.snackbar(view: View, message: String) = Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show() fun View.showSnackbarWithAction( message: String, actionLabel: String, block: () -> Unit ) { Snackbar.make(this, message, Snackbar.LENGTH_INDEFINITE) .setAction(actionLabel) { block() }.show() } } |
You can use this class like this:
1 2 3 4 |
context.shortToast("Some message") context.longToast("Some message") context.showSnackbar(view, "Some message) binding.someViewElement.showSnackbarWithAction("Message", "Action label") |
VisibilityUtil
We use this class to simplify showing and hiding UI elements on screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
object VisibilityUtil { fun View.show() { this.visibility = View.VISIBLE } fun View.hide() { this.visibility = View.INVISIBLE } fun View.remove() { this.visibility = View.GONE } } |
You can use this class like this:
1 2 3 |
binding.someViewElement.show() binding.someViewElement.hide() binding.someViewElement.remove() |
DatePickerUtility
When working with DatePickerDialogs, you can easily extract the logic in a utility class, where you can handle the logic of showing the DatePickerDialog, formatting and displaying the selected date:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
object DatePickerUtil { fun showDatePicker(context: Context, textView: TextView) { val calendar = Calendar.getInstance() val year = calendar.get(Calendar.YEAR) val month = calendar.get(Calendar.MONTH) val day = calendar.get(Calendar.DAY_OF_MONTH) textView.setOnClickListener { val datePickerDialog = DatePickerDialog( context, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> textView.text = formatDate(year, monthOfYear, dayOfMonth) }, year, month, day ) datePickerDialog.show() } } private fun formatDate(year:Int, month:Int, day:Int):String{ val calendar = Calendar.getInstance() calendar.set(year, month, day) val selectedDate = calendar.time return SimpleDateFormat("MMM, dd yyyy").format(selectedDate) } } |
This is just one format, you can use the date format you need. Now to display a DatePickerDialog an show the selected date in a TextView you need just a single line of code:
1 |
showDatePicker(context, binding.someTextView) |
Those are some of the utility classes we use in our projects.
We hope that some of this utility classes will help you or will give you some ideas on how to create your own.
Happy coding!