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.
- <?xml version="1.0" encoding="utf-8"?>
- <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">
- <DatePicker android:id="@+id/firstDatePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00554a" android:datePickerMode="spinner" />
- <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.
- package xyz.rvconstructions.www.datepickerapp;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.DatePicker;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- DatePicker oneDatePicker;
- Button submit;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- oneDatePicker = (DatePicker) findViewById(R.id.firstDatePicker);
- submit = (Button) findViewById(R.id.submitButton);
-
- submit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
-
- String day = "Day = " + oneDatePicker.getDayOfMonth();
- String month = "Month = " + (oneDatePicker.getMonth() + 1);
- String year = "Year = " + oneDatePicker.getYear();
-
- Toast.makeText(getApplicationContext(), day + "\n" + month + "\n" + year, Toast.LENGTH_LONG).show();
- }
- });
- }
- }
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.