How to set date on Date Picker based on other Date Picker in android

In this android programming source code example, we are going to set date on date picker based on other date picker in android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to set date on date picker based on other date picker in android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_centerInParent="true"
        android:text="Start Date"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_below="@id/button1"
        android:layout_marginTop="1dp"
        android:text="End Date"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold" />

</RelativeLayout>

MainActivity.java

import android.app.DatePickerDialog;
import android.app.Dialog;
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.Toast;

import java.util.Calendar;

public class DatePickerAndTimePickerActivity9 extends AppCompatActivity {
    private Button StartDate;
    private Button EndDate;
    int year_y, year_x, month_y, month_x, day_y, day_x;
    private static final int DIALOG_ID_1 = 0, DIALOG_ID_2 = 1;

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

        final Calendar calendar = Calendar.getInstance();
        year_x = calendar.get(Calendar.YEAR);
        month_x = calendar.get(Calendar.MONTH);
        day_x = calendar.get(Calendar.DAY_OF_YEAR);

        year_y = year_x;
        month_y = month_x;
        day_y = day_x+1;

        StartDate = (Button) findViewById(R.id.button1);
        EndDate = (Button) findViewById(R.id.button2);

        StartDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DIALOG_ID_1);
            }
        });

        EndDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DIALOG_ID_2);
            }
        });

    }

    @Override
    protected Dialog onCreateDialog(int id){
        if(id == DIALOG_ID_1){
            return new DatePickerDialog(this, dpd, year_x, month_x, day_x);
        }else if(id == DIALOG_ID_2)
           return new DatePickerDialog(this, dpd1, year_y, month_y, day_y);
        else return null;
    }

    private DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthofyear, int dayofmonth) {
            year_x = year;
            month_x = monthofyear + 1;
            day_x = dayofmonth;
            Toast.makeText(DatePickerAndTimePickerActivity9.this,"StartDate: "+day_x+" / "+month_x+" / "+year_x, Toast.LENGTH_LONG).show();
        }
    };

    private DatePickerDialog.OnDateSetListener dpd1 = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthofyear, int dayofmonth) {
            year_y = year;
            month_y = monthofyear + 1;
            day_y = dayofmonth;
            Toast.makeText(DatePickerAndTimePickerActivity9.this,"EndDate: "+day_y+" / "+month_y+" / "+year_y, Toast.LENGTH_LONG).show();
        }
    };


}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment