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

Android Time Picker

Uploaded by

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

Android Time Picker

Uploaded by

Hyma Thottathyl
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

ANDROID TIMEPICKER

1
Android TimePicker is a component that permits users to select a
time including hour and minute.

TimePicker has two modes with different interface:


•Clock
•Spinner

Clock Mode
Clock mode is the default mode of the TimePicker, in which the
user can visually select the time just like he/ she is adjusting the
time on a clock. Besides, you can also use the keyboard to change
values of hour and minute.

2
TimePicker (Clock Mode)
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="clock" />

3
4
• TimePicker timePicker = (TimePicker) this.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true); // 24H Mode

• timePicker.setIs24HourView(true);

5
Spinner Mode
•In Spinner mode, a TimePicker consists of 2 or 3 Spinners.
These Spinners respectively allows the user to select the hour, the
minute, and either AM or PM.
•TimePicker in Spinner & AM_PM mode consists of
three Spinners, in which the user chooses the hour in the
first Spinner with the values ranging from 1 to 12.
•Next, select the minute in the second Spinner with the values
from 0 to 59;
•Last but not least, select AM/PM in the third spinner.

6
TimePicker (Spinner Mode)
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner" />

7
8
• TimePicker in Spinner & 24H mode only comprises
2 Spinners.
• The user chooses the hour in the first Spinner with
the values ranging from 0 to 23, and
• The minute in the second Spinner with values from 0
to 59.

TimePicker timePicker = (TimePicker)


this.findViewById(R.id.timePicker);
timePicker.setIs24HourView(true); // 24H Mode.

9
10
1. Create a new project on Android Studio:
2. File > New > New Project > Empty Activity
– Name: TimePickerExample
– Package name: com.example.timepickerexample
– Language: Java

11
<?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:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />

12
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker1"
android:layout_marginTop="10dp"
android:layout_marginLeft="160dp"
android:text="Get Time" />

13
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="120dp"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textSize="18dp"/>

</RelativeLayout>

14
public class MainActivity extends AppCompatActivity
{

TimePicker picker;

Button btnGet;

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

15
tvw=(TextView)findViewById(R.id.textView1);

picker=(TimePicker)findViewById(R.id.timePicker1);

picker.setIs24HourView(true);

btnGet=(Button)findViewById(R.id.button1);

16
btnGet.setOnClickListener(new View.OnClickListener()
{
@Override

public void onClick(View v)

{
int hour, minute;

String am_pm;

if (Build.VERSION.SDK_INT >= 23 )
{
hour = picker.getHour();

minute = picker.getMinute();
} 17
else
{

hour = picker.getCurrentHour();

minute = picker.getCurrentMinute();
}

18
if(hour > 12)

{
am_pm = "PM";

hour = hour - 12;

else

am_pm="AM";

}
tvw.setText("Selected Time: "+ hour +":"+ minute+" "+am_pm);
}
});
}
} 19

You might also like