0% found this document useful (0 votes)
22 views6 pages

Android Time & Date Picker Guide

The document discusses two programs that use Timepicker and Datepicker widgets in Android. The first program displays two Timepickers, one with default and one with customized properties. The second program allows the user to select a date and time on clicking buttons, and displays the selection in EditText views.

Uploaded by

mickeymou6825
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)
22 views6 pages

Android Time & Date Picker Guide

The document discusses two programs that use Timepicker and Datepicker widgets in Android. The first program displays two Timepickers, one with default and one with customized properties. The second program allows the user to select a date and time on clicking buttons, and displays the selection in EditText views.

Uploaded by

mickeymou6825
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/ 6

1) Write a program to display use of Timepicker with spinnermode.

=> activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:orientation="vertical"
tools:context=".MainActivity" >
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp" />
<TimePicker
android:layout_width="314dp"
android:layout_height="379dp"
android:layout_x="85dp"
android:layout_y="224dp" />
</AbsoluteLayout>

2) Write a program to display following output.Select and display date and time on clicking.

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


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Date Here" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/et1"
android:layout_alignParentEnd="true"
android:text="SELECT DATE" />
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et1"
android:hint="Time Here" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn1"
android:layout_alignEnd="@id/et2"
android:layout_alignParentEnd="true"
android:layout_x="202dp"
android:layout_y="298dp"
android:text="SELECT TIME" />
</AbsoluteLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btn1, btn2;
EditText et1, et2;
private int mYear, mMonth, mDay, mHour, mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == btn1) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dp = new DatePickerDialog(this, new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
et1.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dp.show();
}
if (v == btn2) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TimePickerDialog tp = new TimePickerDialog(this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
et2.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tp.show();
}
}}

You might also like