Android 11

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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