Android 11
Android 11
Android 11
11
Aim: Develop a program to implement Date and Time Picker.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/pickDateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Date"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/pickTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"
android:layout_below="@id/pickDateButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/selectedDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date"
android:textSize="20sp"
android:layout_below="@id/pickTimeButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<TextView
android:id="@+id/selectedTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Time"
android:textSize="20sp"
android:layout_below="@id/selectedDateTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>
Java Code:
package com.example.myapplication11;
import static
com.example.myapplication11.R.id.selectedDateTextView;
import android.annotation.SuppressLint;
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.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedDateTextView =
findViewById(R.id.selectedDateTextView);
selectedTimeTextView =
findViewById(R.id.selectedTimeTextView);
Button pickDateButton =
findViewById(R.id.pickDateButton);
Button pickTimeButton =
findViewById(R.id.pickTimeButton);
pickDateButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
pickTimeButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog();
}
});
}