Hello Readers! In this post, we are going to learn about how to use android datePicker widget in any android application. We will also go through different attributes of datePicker widget that can be used to customise it.
Output
Getting Started
DatePicker widget can be defined as below –
DatePicker is a widget that provides a display to select any date
Attributes of Android DatePicker Widget
Some of the popular attributes of android datePicker widget are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:calendarTextColor | Defines text color of the calendar |
2 | android:calendarViewShown | Checks whether calendar view is shown or not |
3 | android:datePickerMode | Defines the look of the widget |
4 | android:dayOfWeekBackground | Defines the background color for the header’s day of week |
5 | android:endYear | Defines the last year (inclusive) of the calendar |
6 | android:firstDayOfWeek | Defines the first day of the week according to Calendar |
7 | android:headerBackground | Defines background for the selected date header |
8 | android:maxDate | Defines the maximal date shown by the calendar in mm/dd/yyyy format |
9 | android:minDate | Defines the minimal date shown by the calendar in mm/dd/yyyy format |
10 | android:spinnersShown | Specifies whether spinners are shown or not |
11 | android:startYear | Defines the first year (inclusive) of the calendar |
Some of the popular attributes of DatePicker inherited from FrameLayout are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:foregroundGravity | It defines the gravity of the foreground drawable |
2 | android:measureAllChildren | When measuring, It specifies whether to measure all children or only those that are in visible or invisible state |
Some of the popular attributes of android datePicker inherited from ViewGroup are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animationCache | Defines whether layout animations should create a drawing cache for their children. |
2 | android:layoutAnimation | Defines the layout animation to use the first time the ViewGroup is laid out |
3 | android:layoutMode | Defines the layout mode |
Some of the popular attributes of datePicker inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:fadeScrollbars | Defines whether scrollbar should fade out when not in use or not |
2 | android:fitsSystemWindows | Defines whether to adjust view layout according to system windows. |
3 | android:id | Defines id of the view |
4 | android:minHeight | Defines minimum height |
5 | android:minWidth | Defines minimum width |
6 | android:padding | Defines padding of the view |
7 | android:visibility | Defines visibility of the view |
Example of Android DatePicker Widget
At first, we will create android application. Then, we will use datePicker widget in this application.
1. Creating New Project
Follow steps below to create new project. Please ignore the steps if you have already created a new application.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as DatePicker. Then, click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail. |
Now, we will modify xml and java file to use datePicker widget in the application.
2. Modify Values folder
No values folder have been modified. So, we are not going to mention them here.
3. Use DatePicker Widget in xml file
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:gravity="center" android:orientation="vertical"> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="3dp"/> </LinearLayout>
In activity_main.xml file, we have defined textView and datePicker in android application. Now, we will access these widgets in java file.
4. Access DatePicker Widget in java file
Open src/main/java/com.tutorialwing.datepicker/MainActivity.java file. Then, add below code into it.
package com.tutorialwing.datepicker; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.DatePicker; import android.widget.TextView; import android.widget.Toast; import java.util.Calendar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView textView = findViewById(R.id.textView); DatePicker datePicker = findViewById(R.id.datePicker); if (datePicker != null) { Calendar today = Calendar.getInstance(); datePicker.init(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { String msg = "Selected Date is " + dayOfMonth + "/" + monthOfYear + "/" + year; Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); if (textView != null) { textView.setText(msg); } } }); } } }
In MainActivity.java file, we have accessed textView and datePicker widgets. Then, we have set date change listener to display selected date in textView widget.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.datepicker" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When we run the program, we will get output as shown above.
That’s end of tutorial on Android DatePicker widget.
You must be logged in to post a comment.