0% found this document useful (0 votes)
3 views

practical 16.2

The document contains an XML layout file (activity_main.xml) for an Android application that includes two TextViews and two Buttons for selecting a date and time. Additionally, it features a Java class (MainActivity.java) that implements the functionality to display date and time pickers when the respective buttons are clicked. The selected date and time are then displayed in the corresponding TextViews.

Uploaded by

Pruthesh Upadhye
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

practical 16.2

The document contains an XML layout file (activity_main.xml) for an Android application that includes two TextViews and two Buttons for selecting a date and time. Additionally, it features a Java class (MainActivity.java) that implements the functionality to display date and time pickers when the respective buttons are clicked. The selected date and time are then displayed in the corresponding TextViews.

Uploaded by

Pruthesh Upadhye
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

16.2 activity_main.

xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a date"
android:textSize="18sp"
android:padding="10dp"/>

<Button
android:id="@+id/btnSelectDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"/>

<TextView
android:id="@+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a time"
android:textSize="18sp"
android:padding="10dp"/>

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

16.2 MainActivity.java

package com.example.practical7;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Calendar;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView tvDate, tvTime;


private Button btnSelectDate, btnSelectTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvDate = findViewById(R.id.tvDate);
tvTime = findViewById(R.id.tvTime);
btnSelectDate = findViewById(R.id.btnSelectDate);
btnSelectTime = findViewById(R.id.btnSelectTime);

btnSelectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePicker();
}
});

btnSelectTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTimePicker();
}
});
}

private void showDatePicker() {


final 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(


this, (view, selectedYear, selectedMonth, selectedDay) -> {
String date = selectedDay + "/" + (selectedMonth + 1) + "/" +
selectedYear;
tvDate.setText(date);
}, year, month, day);
datePickerDialog.show();
}

private void showTimePicker() {


final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(


this, (view, selectedHour, selectedMinute) -> {
String time = selectedHour + ":" + String.format("%02d",
selectedMinute);
tvTime.setText(time);
}, hour, minute, true);
timePickerDialog.show();
}
}
16.2 Output

You might also like