19 Program to demonstrate DatePickerDialog and TimePickerDialog in Android Studio

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.blogspot.codingatharva.manualprograms">



<application

    android:allowBackup="true"

    android:icon="@mipmap/ic_launcher"

    android:label="@string/app_name"

    android:roundIcon="@mipmap/ic_launcher_round"

    android:supportsRtl="true"

    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />



            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>

</application>



</manifest>


MainActivity.java:


package com.blogspot.codingatharva.manualprograms;



import android.app.DatePickerDialog;

import android.app.TimePickerDialog;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;

import android.widget.TimePicker;



import java.util.Calendar;



import androidx.appcompat.app.AppCompatActivity;





public class MainActivity extends AppCompatActivity implements View.OnClickListener {



    Button btn1, btn2;

    EditText et1, et2;

    private int mYear, mMonth, mDay, mHour, mMinute;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        btn1 = findViewById(R.id.btn1);

        btn2 = findViewById(R.id.btn2);

        et1 = findViewById(R.id.et1);

        et2 = findViewById(R.id.et2);



        btn1.setOnClickListener(this);

        btn2.setOnClickListener(this);

    }



    @Override

    public void onClick(View v) {

        if (v == btn1) {

            final Calendar c = Calendar.getInstance();

            mYear = c.get(Calendar.YEAR);

            mMonth = c.get(Calendar.MONTH);

            mDay = c.get(Calendar.DAY_OF_MONTH);



            DatePickerDialog dp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                @Override

                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                    et1.setText(dayOfMonth + "-" + (month + 1) + "-" + year);

                }

            }, mYear, mMonth, mDay);



            dp.show();

        }



        if (v == btn2) {

            final Calendar c = Calendar.getInstance();

            mHour = c.get(Calendar.HOUR_OF_DAY);

            mMinute = c.get(Calendar.MINUTE);



            TimePickerDialog tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

                @Override

                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                    et2.setText(hourOfDay + ":" + minute);

                }

            }, mHour, mMinute, false);



            tp.show();



        }

    }

}


activity_main.xml:


<?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:paddingTop="80dp"

    tools:context=".MainActivity">



    <EditText

        android:id="@+id/et1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:hint="Date Here" />



    <Button

        android:id="@+id/btn1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignEnd="@id/et1"

        android:layout_alignParentEnd="true"

        android:text="SELECT DATE" />



    <EditText

        android:id="@+id/et2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/et1"

        android:hint="Time Here" />



    <Button

        android:id="@+id/btn2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/btn1"

        android:layout_alignEnd="@id/et2"

        android:layout_alignParentEnd="true"

        android:text="SELECT TIME" />

</RelativeLayout>


Output:

Previous
Next Post »