0% found this document useful (0 votes)
41 views9 pages

Android 11

The document describes a program to implement a date and time picker in Android. It includes XML code to design the layout with buttons to pick date and time, and text views to display the selections. The Java code handles button clicks to show date and time picker dialogs, and updates the text views with the selections. When the buttons are clicked, date and time picker dialogs are displayed to allow selecting the date and time, which are then shown in the text views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

Android 11

The document describes a program to implement a date and time picker in Android. It includes XML code to design the layout with buttons to pick date and time, and text views to display the selections. The Java code handles button clicks to show date and time picker dialogs, and updates the text views with the selections. When the buttons are clicked, date and time picker dialogs are displayed to allow selecting the date and time, which are then shown in the text views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Practical No.

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;

public class MainActivity extends AppCompatActivity {

TextView selectedDateTextView, selectedTimeTextView;

@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();
}
});
}

private void showDatePickerDialog() {


Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth =
calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new
DatePickerDialog(
this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int month, int dayOfMonth) {
String date = dayOfMonth + "/" + (month + 1) +
"/" + year;
selectedDateTextView.setText(date);
}
},
year, month, dayOfMonth);
datePickerDialog.show();
}

private void showTimePickerDialog() {


Calendar calendar = Calendar.getInstance();
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new
TimePickerDialog(
this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int
hourOfDay, int minute) {
String time = hourOfDay + ":" + minute;
selectedTimeTextView.setText(time);
}
},
hourOfDay, minute, true);
timePickerDialog.show();
}
}
Output:
1)
2)

You might also like