Practical No 16
Practical No 16
16
Practical No. 16: Develop a program to implement Date and Time Picker
I. Practical Significance
Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs.
Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or
date (month, day, year). Using these pickers helps ensure that your users can pick a time or
date that is valid, formatted correctly, and adjusted to the user's locale.
Time Picker:
Android Time Picker allows you to select the time of day in either 24 hour or AM/PM mode.
The time consists of hours, minutes and clock format. Android provides this functionality
through TimePicker class. Following xml attribute is used to create time picker.
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />
2. TimePicker
i. setHour(Integer Hour):
This method is used to set the current hours in a time picker.
ii. getHour():
This method is used to get the current hours from a time picker.
iii. setMinute(int minute)
Sets the currently selected minute.
iv. getMinute():
This method is used to get the current minutes from a time picker.
v. setIs24HourView(Boolean is24HourView):
This method is used to set the mode of the Time picker either 24 hour mode or AM/PM mode.
vi. is24HourView()
vii. setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)
Set the callback that indicates the time has been adjusted by the user.
3. DatePicker methods:
i. setSpinnersShown(boolean shown):
This method is used to set whether the spinner of the date picker in shown or not. In this
method you have to set a Boolean value either true or false. True indicates spinner is shown,
false value indicates spinner is not shown. Default value for this function is true.
ii. getDayOfMonth():
This method is used to get the selected day of the month from a date picker. This method
returns an integer value.
iii. getMonth():
This method is used to get the selected month from a date picker. This method returns an
integer value.
iv. getYear():
This method is used to get the selected year from a date picker. This method returns an integer
value.
v. getFirstDayOfWeek():
This method is used to get the first day of the week. This method returns an integer value.
X. Exercise
(Use blank space provide for answers or attached more pages if needed)
1. Write a program to display following output. Use TimePicker with Spinnermode.
2. Write a program to display following output. Select and display date and time on click of
“select date”, “select time” buttons respectively.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
//=================================================================
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
android:layout_margin="10dp"
android:background="#f77"/>
<TimePicker
android:id="@+id/timePicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:timePickerMode="spinner"
android:layout_toRightOf="@+id/timePicker1"
android:background="#77ffff"/>
<TimePicker
android:id="@+id/timePicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker1"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:timePickerMode="clock" />
</RelativeLayout>
//=================================================================
2.
MainActivity.java
package com.jpa.experiment16_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.editTextDate);
et2 = findViewById(R.id.editTextTime);
b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = "Current Date : " + sdf.format(c.getTime());
et1.setText(date);
}
});
}
}
//=================================================================
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="12"
android:inputType="date"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp" />
<EditText
android:id="@+id/editTextTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="12"
android:inputType="time"
android:layout_below="@+id/editTextDate"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date"
android:layout_toRightOf="@+id/editTextDate"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time"
android:layout_toRightOf="@+id/editTextTime"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/button1" />
</RelativeLayout>