How to Implement Date Range Picker in Android?
Last Updated :
04 Jul, 2023
Date Range Picker is a widely used feature in many popular Android apps and an essential component of Material Design. It allows users to select a range of dates such as a start and end date for various purposes including scheduling, filtering data, and setting time boundaries. Some Of The Real Life Examples Of Date Range Picker are when we use some banking mobile applications and if we want to download the bank statement of our account for some particular date range then at that Date Range Picker is used. A sample video is given below to get an idea about what we are going to do in this article.
Note: This Android article covered in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Add Material Design Dependency in build.grade(Module)
For Using Material Date Picker in the Project.
XML
dependencies {
implementation 'com.google.android.material:material:1.9.0'
}
Step 3: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know it in detail. We have created one simple UI with two text views one as a banner and one for setting the date that is selected by the user and we have also added one button that will show the date picker dialog.
activity_main.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<TextView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/app_name"
android:textAlignment="center"
android:textSize="25sp"
android:textColor="#F6F6"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="16dp" />
<TextView
android:id="@+id/selectedDate"
android:layout_width="184dp"
android:layout_height="42dp"
android:layout_marginTop="384dp"
android:textAlignment="center"
android:textColor="@color/black"
android:text="Selected Date Rnage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="172dp"
android:text="Open Dialog"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Working with the MainActivity file
Navigate to app > java > your app's package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail.
Java
package com.example.gfgapp;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Pair;
import com.google.android.material.datepicker.MaterialDatePicker;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
TextView selectedDate;
Button datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedDate = findViewById(R.id.selectedDate);
datePicker = findViewById(R.id.datePicker);
// Setting click listener for the date picker button
datePicker.setOnClickListener(view -> DatePickerdialog());
}
private void DatePickerdialog() {
// Creating a MaterialDatePicker builder for selecting a date range
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
builder.setTitleText("Select a date range");
// Building the date picker dialog
MaterialDatePicker<Pair<Long, Long>> datePicker = builder.build();
datePicker.addOnPositiveButtonClickListener(selection -> {
// Retrieving the selected start and end dates
Long startDate = selection.first;
Long endDate = selection.second;
// Formating the selected dates as strings
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String startDateString = sdf.format(new Date(startDate));
String endDateString = sdf.format(new Date(endDate));
// Creating the date range string
String selectedDateRange = startDateString + " - " + endDateString;
// Displaying the selected date range in the TextView
selectedDate.setText(selectedDateRange);
});
// Showing the date picker dialog
datePicker.show(getSupportFragmentManager(), "DATE_PICKER");
}
}
Kotlin
package com.example.gfgapp
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.util.Pair
import com.google.android.material.datepicker.MaterialDatePicker
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
class MainActivity : AppCompatActivity() {
private lateinit var selectedDate: TextView
private lateinit var datePicker: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
selectedDate = findViewById(R.id.selectedDate)
datePicker = findViewById(R.id.datePicker)
// Setting click listener for the date picker button
datePicker.setOnClickListener { datePickerDialog() }
}
private fun datePickerDialog() {
// Creating a MaterialDatePicker builder for selecting a date range
val builder = MaterialDatePicker.Builder<Pair<Long, Long>>.dateRangePicker()
builder.setTitleText("Select a date range")
// Building the date picker dialog
val datePicker = builder.build()
datePicker.addOnPositiveButtonClickListener { selection ->
// Retrieving the selected start and end dates
val startDate = selection.first
val endDate = selection.second
// Formatting the selected dates as strings
val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val startDateString = sdf.format(Date(startDate))
val endDateString = sdf.format(Date(endDate))
// Creating the date range string
val selectedDateRange = "$startDateString - $endDateString"
// Displaying the selected date range in the TextView
selectedDate.text = selectedDateRange
}
// Showing the date picker dialog
datePicker.show(supportFragmentManager, "DATE_PICKER")
}
}
Output:
Similar Reads
How to Implement Country Code Picker in Android?
Country Code Picker (CCP) is an android library that helps users to select country codes (country phone codes) for telephonic forms. CCP provided a UI component that helps the user to select country codes, country flags, and many more in an android spinner. It gives well-designed looks to forms on t
3 min read
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
How to Implement MultiSelect DropDown in Android?
In this article, we are going to see how we can make a MultiSelect DropDown in android studio and will select multiple items from a dropdown list. Advantages of MultiSelect DropDown. It is a good replacement for list boxes as it uses less space does the same work as a list box and gives a good look
4 min read
How to Implement Custom Searchable Spinner in Android?
Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
5 min read
How to Implement Custom Calendar in Android?
Nowadays in most of the apps, we see Calendar in most of the apps for displaying birth dates or for any appointment application. Displaying the date in the app with the help of the Calendar view gives a better user experience. In this article, we are going to make a custom calendar for our android a
4 min read
Material Design Date Range Picker in Android using Kotlin
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro
3 min read
Slider Date Picker in Android
Slider Date Picker is one of the most popular features that we see in most apps. We can get to see this Slider date picker in most of the travel planning applications, Ticket booking services, and many more. Using Slider date Picker makes it efficient to pick the date. In this article, we are going
3 min read
Implement customized TimePicker in Android using SnapTimePicker
Android TimePicker is a user interface control for selecting the time in either 24-hour format or AM/PM mode. It is used to ensure that users pick a valid time for the day in the application. The default TimePicker can be customized by using the SnapTimePicker in Android.Features of SnapTimePickerSo
3 min read
Auto Image Slider in Android with Example
Auto Image Slider is one of the most seen UI components in Android. This type of slider is mostly seen inside the apps of big E-commerce sites such as Flipkart, Amazon, and many more. Auto Image Slider is used to represent data in the form of slides that change after a specific interval of time. In
6 min read
Time Picker Dialog in Android
Android TimePicker is a user interface control for selecting the time in either 24-hour format or AM/PM mode. It is used to ensure that users pick the valid time for the day in our application. The time picker interface exists basically in two modes one is under XML layout and another is a dialog. I
3 min read