DEV Community

Maria Luíza
Maria Luíza

Posted on

Date & Time Picker with Compose

Firebase icon playng guitar

Hello, amazing person of the internet! Hope you're doing well.

In today's world, where every second counts, managing dates and times efficiently in our app has become crucial.

 

Data Picker

By default, a date picker enables you to select a date through a calendar UI. Nevertheless, it also permits switching to a date input mode for manually entering dates using the keyboard's numeric keys.

The first thing we have to do is create a variable responsible for keeping the state of our calendar:

Let's call the DatePicker() function to display the date block view:

You are going to see something like that:

Date Picker where you can set the date and year

We can make things look more beautiful and interactive.To create an amazing dialog, we can call the function DatePickerDialog(). As the name suggests, this allows us to create a dialog and control when it will be displayed:

I am going to create a variable that will receive a boolean to control when we are going to show the dialog. When the user clicks on the dismiss button, iwill set it to false, and do the same thing when the user confirms:

Now, let's update our variable state cleaning its value and call the DatePicker() into our DatePickerDialog() function:

You're going to see something like that:

Date Picker Dialog where you can set the date and year

To determine the date the user has selected, simply call the *state * variable, and it will provide you with a set of options:

To control the color of the dialog just pass the parameter colors:

 

Time Picker

Time pickers help users select and set a specific time. The logic is very similar to the Date Picker. We need to create a state and call the dialog function:

You're going to see something like that:

Time Picker where you can select the hour and minute

Like the Date Picker we can make things look more beautiful and interactive. To create other types of timers, we can call the function TimeInput(). This enables us to input a specific time and control when it will be displayed:

You're going to see something like that:

Time Picker where you can set the hour and minute

When this article was posted, the material io 3 didn't support the TimePickerDialog. However, the next release will enable the display of a dialog box similar to the DatePickerDialog.

 

Conclusion

You can also customize the colors of the pickers as you desire. The Date and Time Picker is an essential design element for your app, and it's crucial to understand how to utilize this feature correctly.

All the code and exemple it is on this repository.

Happy coding ❤


Please let me know what you think in the comments…

Connect with me 👇

Linkedin

GitHub

Medium

gif of a lamp

Top comments (0)