Android program to implement DatePicker

Android program to implement DatePicker

Android program to implement DatePicker. DatePicker is a widget used to select the date. For example, when you use an android app and then they ask to set your birthdate, then they use the datepicker widget. You can set the datePickerMode to spinner or calendar.

Methods associated with DatePicker are:-

In the code given below, I will be showing you both the modes spinner as well as calendar mode for your datepicker. Only xml files are different MainActivity.java file will be the same. It is entirely on the methods given above, so if you know the methods for datePicker, implementing it will be very easy.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “DatePicker” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • Copy the contents of res/layout/activity_main.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(res/layout/activity_main.xml):(For spinner mode)

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="codedost.datepicker.MainActivity">

    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:scaleX="1"
        android:scaleY="1"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"
        android:id="@+id/datePicker" />

    <Button
        android:text="Display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/datePicker"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_marginTop="20dp"
        android:textSize="24sp"
        android:id="@+id/textview"
        android:gravity="center"/>

</RelativeLayout>

XML File(res/layout/activity_main.xml):(For Calendar mode)

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="codedost.datepicker.MainActivity">

    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-25dp"
        android:scaleX=".9"
        android:scaleY=".7"
        android:calendarTextColor="#44ff00"
        android:datePickerMode="calendar"
        android:id="@+id/datePicker" />

    <Button
        android:text="Display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/datePicker"
        android:layout_marginTop="-25dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_marginTop="20dp"
        android:textSize="24sp"
        android:id="@+id/textview"
        android:gravity="center"/>

</RelativeLayout>

MainActivity.java

package codedost.datepicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    DatePicker dp;
    Button b1;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dp=(DatePicker) findViewById(R.id.datePicker);
        b1=(Button) findViewById(R.id.button);
        tv=(TextView) findViewById(R.id.textview);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb=new StringBuffer();
                sb.append(dp.getDayOfMonth()+"/");
                sb.append((dp.getMonth()+1)+"/");
                sb.append(dp.getYear());

                tv.setText(sb.toString());
            }
        });
    }
}

Output

 

Share Me!