Android DatePicker Control

Android DatePicker Control is a control to show date to the user. As the name suggests, DatePicker control is used to set date in the form of year, month, and day.

Usage

The DataPicker control can be used to show a calendar to select Date of Birth or Company Established Date or degree completion date, etc.

Android DatePicker Control Example

Now, we will write the code for our example app, which has a TextView, Button and DatePicker.

If youā€™re beginning with Android, then learn how to run your first Android app in Android Studio.

Here, we will be adding the Graduation Date using the Android DatePicker control.

Follow the below code,

MainActivity.java

The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here we will set the View to be displayed with the setContentView.

On button click, the date we selected using the Android DatePicker control will be displayed using Toast. Toast in Android is a notification message visible in the form of popup.

activity_main.xml

For src\main\res\layout\activity_main.xml,

Weā€™ll add our UI content here, 1 TextView, 1 Button and 1 DatePicker. On button click, the graduation date you entered using the DatePicker will get displayed. For displaying the graduation date here, weā€™ve used Toast,

AndroidManifest.xml

The content of \src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,

strings.xml

Hereā€™s the code for res/values/strings.xml. The app name and any hardcoded strings will be visible. We hardcoded string ā€œStudyopedia DatePicker Applicationā€ and used @string/title in activity_main.xml. Here, it gets mentioned under <resources>.

We also hardcoded the detail string and used @string/detail,

Output

We created an AVD, so now we will run our app using the same AVD. Go to Run > Run app or press the run button or Shift + f10. This will install your app on the AVD and the following emulator with your app opens up,

Android DatePicker Control

Now, select the date from the DatePicker control,

Android DatePicker Control Output

After selecting the date, click on the button i.e. GRADUATION DATE,

Android DatePicker Control Output showing result

 


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Android Gravity
Android Tutorial
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading