0% found this document useful (0 votes)
31 views3 pages

Practical 16

The document outlines the implementation of a Date and Time Picker in an Android application using XML for the layout and Java for the functionality. It includes the layout structure with buttons and edit texts for selecting date and time, as well as the Java code to handle the date and time selection events. The code utilizes DatePickerDialog and TimePickerDialog to display the respective pickers and update the UI with the selected values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Practical 16

The document outlines the implementation of a Date and Time Picker in an Android application using XML for the layout and Java for the functionality. It includes the layout structure with buttons and edit texts for selecting date and time, as well as the Java code to handle the date and time selection events. The code utilizes DatePickerDialog and TimePickerDialog to display the respective pickers and update the UI with the selected values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical 16

❖ Date & Time Picker.


<?xml version="1.0" encoding="utf-8"?> <Button
<RelativeLayout xmlns:android="http://schemas. android:layout_width="wrap_content"
android.com/apk/res/android" android:layout_height="wrap_content"
android:text="SELECT DATE"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/btn_date"
android:layout_width="match_parent" android:layout_alignBottom="@+id/in_date"
android:layout_height="match_parent" android:layout_toRightOf="@+id/in_date"
android:layout_margin="10dp" android:layout_toEndOf="@+id/in_date" />
tools:context=".MainActivity">
<EditText
<TextView android:layout_width="200dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:id="@+id/in_time"
android:text="Select Date and Time" android:layout_below="@+id/in_date"
android:textAlignment="center" android:layout_alignParentLeft="true"
android:textSize="20dp" android:layout_alignParentStart="true" />
android:layout_marginTop="20dp" />
<Button
<EditText android:layout_width="wrap_content"
android:layout_width="200dp" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="SELECT TIME"
android:id="@+id/in_date" android:id="@+id/btn_time"
android:layout_marginTop="100dp" android:layout_below="@+id/btn_date"
android:layout_alignParentTop="true" android:layout_alignLeft="@+id/btn_date"
android:layout_alignParentLeft="true" android:layout_alignStart="@+id/btn_date"/>
android:layout_alignParentStart="true" /> </RelativeLayout>

➢ Java File:-
package com.example.datetimepickerprac16; mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
import android.app.DatePickerDialog; mDay = c.get(Calendar.DAY_OF_MONTH);
import android.app.TimePickerDialog;
import android.os.Bundle; DatePickerDialog datePickerDialog = new
import android.view.View; DatePickerDialog(this, new
import android.widget.Button; DatePickerDialog.OnDateSetListener() {
import android.widget.DatePicker;
import android.widget.EditText; @Override
import android.widget.TimePicker; public void onDateSet(DatePicker view,
import androidx.activity.EdgeToEdge; int year,
import androidx.appcompat.app.AppCompatActivity; int monthOfYear, int
import androidx.core.graphics.Insets; dayOfMonth) {
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; txtDate.setText(dayOfMonth + "-" +
import java.util.Calendar; (monthOfYear + 1) + "-" + year);

public class MainActivity extends AppCompatActivity }


implements }, mYear, mMonth, mDay);
View.OnClickListener datePickerDialog.show();
{ }
Button btnDatePicker, btnTimePicker; if (v == btnTimePicker) {
EditText txtDate, txtTime;
private int mYear, mMonth, mDay, mHour, // Get Current Time
mMinute; final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
@Override mMinute = c.get(Calendar.MINUTE);
protected void onCreate(Bundle savedInstanceState)
{ // Launch Time Picker Dialog
super.onCreate(savedInstanceState); TimePickerDialog timePickerDialog = new
setContentView(R.layout.activity_main); TimePickerDialog(this, new
TimePickerDialog.OnTimeSetListener() {
btnDatePicker=(Button)findViewById(R.id.btn_date);
btnTimePicker=(Button)findViewById(R.id.btn_time); @Override
txtDate=(EditText)findViewById(R.id.in_date); public void onTimeSet(TimePicker
txtTime=(EditText)findViewById(R.id.in_time); view, int hourOfDay,
btnDatePicker.setOnClickListener(this); int minute) {
btnTimePicker.setOnClickListener(this);
} txtTime.setText(hourOfDay + ":" +
minute);
@Override }
public void onClick(View v) { }, mHour, mMinute, false);
if (v == btnDatePicker) { timePickerDialog.show();
}
// Get Current Date }
final Calendar c = Calendar.getInstance(); }

➢ Output:-

You might also like