
What is TimePickerDialog ?
TimePicker Dialog is a selected of time day,24 hours and AM/PM. The time consists of hours, minutes and clock format. Android provides this functionality through TimePicker class.
TimePicker to set time is hour and minutes. it can not selected or set time in seconds.
Methods of TimePickerDialog
setCurrentHour(Integer currentHour) : In this method used to set current hour.it is one parameter to set hour.
setCurrentMinute(Integer currentMinute) : In this method used to set current Minute.it is required one parameter set to minute.
is24HourView() : In this method to returns true if this is in 24 hour view else false.
setIs24HourView(Boolean is24HourView) : In this method used to Set 24 hour or AM/PM mode.
setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener) : In this method use it can listener when you can set the time on timepicker dialog this method is called.
How to Create TimePickerDialog with Example ?
Create activity_main.xml File following this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.bhaumik.MainActivity"> <TextView android:id="@+id/tv_your_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your Time" android:textSize="24dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:textColor="@android:color/background_dark"/> <Button android:id="@+id/btn_select_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Select Time" android:layout_marginTop="20dp" android:textAllCaps="false" /> </LinearLayout> |
Create MainActivity.java File Follow this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package com.bhaumik; import android.app.TimePickerDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; import java.util.Calendar; public class MainActivity extends AppCompatActivity { Button button; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.btn_select_time); textView = (TextView) findViewById(R.id.tv_your_time); Calendar calendar = Calendar.getInstance(); final int hour = calendar.get(Calendar.HOUR_OF_DAY); final int minute = calendar.get(Calendar.MINUTE); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int hours, int minuts) { String str_hours; if(hours<10){ str_hours = "0" + hours; } else { str_hours = "" + hours; } String str_minute; if(minuts < 10){ str_minute = "0" + minuts; } else { str_minute = "" + minuts; } String status = "AM"; if(hours > 11){ status = "PM"; } int hours_12; if(hours > 11){ hours_12 = hours - 12; }else { hours_12 = hours; } String time = hours_12 + " : " + str_minute + " : " + status; textView.setText(time); } },hour,minute,false); timePickerDialog.show(); } }); } } |
In this Exmple is Create XML of One TextView to display date when you can selected timepicker dialog.And Then One Button when you can click to display timepicker dialog.Now i have implement id of button and textview and i have create calendar class object.calendar is used to get the current date time.and then create two integer variable is hour and minute. in this variable store to get calendar current hour and minute date.now i have implement button click listener and create TimePickerDialog object.
when you can create TimePickerDialog class object to give two parameter first is context means which activity to diaplay dialog.and second is dialog listener of onTimeSetListener.in this method use of when you can select hour and minute on the timepicker dialog and then click OK to display time you want to selected.