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:

You can use this class to check if the internet is available like this:

 InputValidatorUtility

We use this utility class to check if a mandatory input field is empty or not:

You can use this class to check an input field like this:

InputViewHandler

We use this utility class to hide the keyboard and to show the cursor on edit text elements on touch events:

You can use it like this:

MessageUtility

We use this class to show different kinds of messages to the user. We can show toast messages, snackbar, snackbar with action.

You can use this class like this:

VisibilityUtil

We use this class to simplify showing and hiding UI elements on screen.

You can use this class like this:

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:

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:

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!

 

Spread the love

Leave a Reply

Your email address will not be published.