0% found this document useful (0 votes)
25 views4 pages

Practical 16.2 (1) CK

The document outlines a practical assignment for developing a Date and Time Picker program in Android. It includes the XML layout code for the user interface and the Java code for the MainActivity that handles date and time selection. The program features buttons to select the date and time, displaying the selected values in EditText fields.
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)
25 views4 pages

Practical 16.2 (1) CK

The document outlines a practical assignment for developing a Date and Time Picker program in Android. It includes the XML layout code for the user interface and the Java code for the MainActivity that handles date and time selection. The program features buttons to select the date and time, displaying the selected values in EditText fields.
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/ 4

Practical No.

16

Name :- Chaitanya Kumavat

Roll no. 56

Practical Name:- Develop a program to implement Date and Time Picker.

activity_main.xml code:-

<?xml version="1.0" encoding="utf-8"?>


<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:padding="20dp"
tools:context=".MainActivity">

<!-- TextView for Name -->


<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amit Morade"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center"
android:layout_marginBottom="20dp"/>

<!-- EditText for Date Display -->


<EditText
android:id="@+id/editTextDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Selected Date"
android:focusable="false"/>

<Button
android:id="@+id/btnSelectDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"
android:layout_marginTop="10dp"/> <!--
EditText for Time Display -->
<EditText
android:id="@+id/editTextTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Selected Time"
android:focusable="false"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btnSelectTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:layout_marginTop="10dp"/>

</LinearLayout> MainActivity.java
code:-

package com.example.practical16;

import android.app.DatePickerDialog; import


android.app.TimePickerDialog; import
android.os.Bundle; import android.view.View; import
android.widget.Button; import
android.widget.EditText; import
android.widget.TextView; import
android.widget.Toast; import
androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

EditText editTextDate, editTextTime;


Button btnSelectDate, btnSelectTime;
TextView textViewName;

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

// Initialize Views
textViewName = findViewById(R.id.textViewName);
editTextDate = findViewById(R.id.editTextDate); editTextTime
= findViewById(R.id.editTextTime); btnSelectDate =
findViewById(R.id.btnSelectDate); btnSelectTime =
findViewById(R.id.btnSelectTime);

// Date Picker
btnSelectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR); int month
= calendar.get(Calendar.MONTH); int day =
calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,


(view, selectedYear, selectedMonth, selectedDay) ->
editTextDate.setText(selectedDay + "/" + (selectedMonth + 1) + "/" +
selectedYear),
year, month, day);
datePickerDialog.show();
}
});

// Time Picker
btnSelectTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,


(view, selectedHour, selectedMinute) ->
editTextTime.setText(selectedHour + ":" + String.format("%02d",
selectedMinute)),
hour, minute, true);
timePickerDialog.show();
}
});
}}
Out
put:
-

You might also like