DatePicker & TimePicker

R.layout.single_item

The time picker and date picker dialog will open when you click the button. The method is given below.

The date picker will pop up the dialog which is used to set the date

The time picker will pop up the dialog which is used to set the time

we can select the date of the month and year in android date picker and time picker shown below

MainActivity[MainActivity.class]

package com.androidfeeders.timeanddate;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
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.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {
    Button setBtnDatePicker, setGetBtnTimePicker;
    TextView textViewDisplay;
    int mYear, mMonth, mDay, mHour, mMinute;

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

        setBtnDatePicker=findViewById(R.id.set_date);
        setGetBtnTimePicker=findViewById(R.id.set_time);
        textViewDisplay=findViewById(R.id.display);


        setBtnDatePicker.setOnClickListener(this);
        setGetBtnTimePicker.setOnClickListener(this);

    }

    
    @Override
    public void onClick(View view) {

        int getView = view.getId();

        Calendar calendarInstance = Calendar.getInstance();
        switch (getView) {

            // set Date

            case R.id.set_date:

                //Getting calendar Year , Month ,Day
                mYear = calendarInstance.get(Calendar.YEAR);
                mMonth = calendarInstance.get(Calendar.MONTH);
                mDay = calendarInstance.get(Calendar.DAY_OF_MONTH);


                //Date Picker
                DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {

                                textViewDisplay.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                                Toast.makeText(getApplicationContext(),"Selected Date is "+dayOfMonth+ "/" + (monthOfYear + 1) + "/" + year,Toast.LENGTH_SHORT). show();

                            }
                        }, mYear, mMonth, mDay);
                datePickerDialog.show();
                break;

            // set Time

            case R.id.set_time:

                //Getting calendar Hour , Minute
                mHour = calendarInstance.get(Calendar.HOUR_OF_DAY);
                mMinute = calendarInstance.get(Calendar.MINUTE);

                //Time Picker
                TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                        new TimePickerDialog.OnTimeSetListener() {

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

                                textViewDisplay.setText(hourOfDay + ":" + minute);
                                Toast.makeText(getApplicationContext(),"Selected Time is "+hourOfDay + ":" + minute,Toast.LENGTH_SHORT). show();

                            }
                        }, mHour, mMinute, false);
                timePickerDialog.show();
                break;
        }

    }


}

Layout Resource[activity_main.xml]

<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:layout_margin="10dp"
    tools:context=".MainActivity">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/set_date"
        android:layout_margin="10dp"
        android:background="@android:color/holo_green_light"
        android:id="@+id/set_date" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/set_time"
        android:layout_margin="10dp"
        android:background="@android:color/holo_green_light"
         android:id="@+id/set_time" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="40dp"
        android:textSize="20dp"
        android:text="@string/display"
        android:id="@+id/display" />

</LinearLayout>

Gradle Scripts~build.gradle[Module:app]

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.androidfeeders.timeanddate"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Gradle Scripts~build.gradle[Project:AndroidstudioSampleCode]

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Resource[strings.xml]

<resources>
    <string name="app_name">TimeandDate</string>
    <string name="set_date">Set Date</string>
    <string name=q"set_time">Set Time</string>
    <string name="display">Display</string>
</resources>

manifests

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidfeeders.timeanddate">

    <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>

Ramasamy

I started to learn android and Kotlin and iOS and Hybrid application and i cant code IOS very well. But i love more interesting to code and finding new developer minds who are join to me You're welcome to keep on using my website.Instead, you can join our FB page for developer comments and post there: https://www.facebook.com/ramasamy.m.779 Best wishes :) Ramasamy(Software Developer)

Android Development and IOS Development & React Native Application

See what's new in Android & IOS & Hybrid development … explore and learn in site

About Ramasamy

Kotlin and React native example too

Related Posts

3 Comments

Ramasamy

5 min ago

Kotlin and React native example too

Reply

Ramasamy

5 min ago

Kotlin and React native example too

Reply

Ramasamy

5 min ago

Kotlin and React native example too

Reply

Leave a reply