Create A DatePicker App In Android Application Using Android Studio

Introduction

This blog helps to explain how to develop a DatePicker app in an Android Application, using Android Studio.


Android

Here, you can create your Application name and choose where your project is stored on the location and click Next button.

Android

Now, we can select the version of an Android; it is Target Android Devices.

Android

Here, we can add the activity and click Next button.

Android

Now, we can write the activity name and click Finish button.

Android

Now, open your project and you will go to the activity_main.xml and afterwards, you will build the design. You should choose the toolbox and if you want some options (DatePicker, button), use the drag and drop method.

Android

Now, we can see the Graphical User Interface design.

Android

Here, you need to build on the design and write .XML code.

activity_mai.xml code.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.datepickerapp.MainActivity">  
  3.     <DatePicker android:id="@+id/firstDatePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00554a" android:datePickerMode="spinner" />  
  4.     <Button android:id="@+id/submitButton" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_below="@+id/firstDatePicker" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:background="#554800" android:text="submit" android:textColor="#fff" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout>  
Now, you will go to the MainActivity.java page and build Java code.

First of all, you will declare a file, which is an extension file.

Android

Now, we can see MainActivity.java code.
  1. package xyz.rvconstructions.www.datepickerapp;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.MenuItem;  
  6. import android.view.View;  
  7. import android.widget.DatePicker;  
  8. import android.widget.Button;  
  9. import android.widget.Toast;  
  10. public class MainActivity extends AppCompatActivity {  
  11.     DatePicker oneDatePicker;  
  12.     Button submit;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         oneDatePicker = (DatePicker) findViewById(R.id.firstDatePicker);  
  18.         submit = (Button) findViewById(R.id.submitButton);  
  19.         // perform click event on submit button    
  20.         submit.setOnClickListener(new View.OnClickListener() {  
  21.             @Override  
  22.             public void onClick(View v) {  
  23.                 // get the values for day of month , month and year from a date picker    
  24.                 String day = "Day = " + oneDatePicker.getDayOfMonth();  
  25.                 String month = "Month = " + (oneDatePicker.getMonth() + 1);  
  26.                 String year = "Year = " + oneDatePicker.getYear();  
  27.                 // display the values by using a toast    
  28.                 Toast.makeText(getApplicationContext(), day + "\n" + month + "\n" + year, Toast.LENGTH_LONG).show();  
  29.             }  
  30.         });  
  31.     }  
  32. }  
Step 7

Here, you will go to run it and select Run-> Run app option.

Android

Here, you will choose Emulator or the devices; it is Nokia Nokia _X.

Android

Here, you can see the output.

Android

Now, you can select some date and click the submit button. Afterwards, you will see the output given below.

Android