{"id":1096,"date":"2016-03-01T10:19:52","date_gmt":"2016-03-01T10:19:52","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1096"},"modified":"2019-06-12T11:32:00","modified_gmt":"2019-06-12T11:32:00","slug":"timepicker","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/timepicker","title":{"rendered":"TimePicker Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, TimePicker is a widget used for selecting the time of the day in either AM\/PM mode or 24 hours mode. The displayed time consist of hours, minutes and clock format. \u00a0If we need to show this view as a Dialog then we have to use a TimePickerDialog class.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1148\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-in-Android.jpg\" alt=\"TimePicker in Android\" width=\"644\" height=\"225\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-in-Android.jpg 644w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-in-Android-300x105.jpg 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/center><strong>TimePick<\/strong><strong>er code:<\/strong><\/p>\n<pre>&lt;TimePicker\r\nandroid:id=\"@+id\/simpleTimePicker\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:timePickerMode=\"spinner\"\/&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Methods of TimePicker:<\/strong><\/h4>\n<p>Let\u2019s discuss some common methods of a time picker, which are used to configure a time picker in our application.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. setCurrentHour(Integer currentHour):<\/strong><\/span><\/p>\n<p>This method is used to set the current hours in a time picker.<\/p>\n<p><strong>setHour(Integer hour):<\/strong><em>\u00a0setCurrentHour()\u00a0method was deprecated in API level 23. From api level 23 we\u00a0have to use <strong>setHour(Integer hour)<\/strong><\/em>. In this method there is only one parameter of integer type which is\u00a0used to set the value for hours.<\/p>\n<p>Below we set the 5 value for the current hours.<\/p>\n<pre>TimePicker simpleTimePicker=(TimePicker)findViewById(R.id.simpleTimePicker); \/\/ initiate a time picker\r\n\/\/ set the value for current hours\r\nsimpleTimePicker.setCurrentHour(5); \/\/ before api level 23\r\nsimpleTimePicker.setHour(5); \/\/ from api level 23\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1099\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/setcurrentHour-setHour-in-TimePicker-Android.jpg\" alt=\"setcurrentHour setHour in TimePicker Android\" width=\"190\" height=\"220\" \/><\/center><span style=\"color: #008000;\"><strong>2. setCurrentMinute(Integer currentMinute):<\/strong><\/span><\/p>\n<p>This method is used to set the current minutes in a time picker.<\/p>\n<p><strong> setMinute(Integer minute):<\/strong><em>\u00a0setCurrentMinute() method was deprecated in API level 23. From api level 23 we\u00a0have to use <strong>setMinute(Integer minute)<\/strong><\/em>. In this method there is only one parameter of integer type which\u00a0set the value for minutes.<\/p>\n<p>Below we set the 35 value for the current minutes.<\/p>\n<pre>TimePicker simpleTimePicker=(TimePicker)findViewById(R.id.simpleTimePicker); \/\/ initiate a time picker\r\n\/\/ set the value for current hours\r\nsimpleTimePicker.setCurrentMinute(35); \/\/ before api level 23\r\nsimpleTimePicker.setMinute(35); \/\/ from api level 23\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1100\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/setMinute-setCurrentMinute-in-Timepicker-Android.jpg\" alt=\"setMinute setCurrentMinute in Timepicker Android\" width=\"367\" height=\"280\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/setMinute-setCurrentMinute-in-Timepicker-Android.jpg 367w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/setMinute-setCurrentMinute-in-Timepicker-Android-300x229.jpg 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/center><span style=\"color: #008000;\"><strong>3. getCurrentHour():<\/strong><\/span><\/p>\n<p>This method is used to get the current hours from a time picker.<\/p>\n<p><strong>getCurrentHour():<\/strong><em>\u00a0getCurrentHour() method was deprecated in API level 23. From api level 23 you have to use <strong>getHour(). <\/strong><\/em>This method returns\u00a0 an integer value.<\/p>\n<p>Below we get the value of hours from a timepicker set by user.<\/p>\n<pre>TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); \/\/ initiate a time\r\npickerint hours =simpleTimePicker.getCurrentHour(); \/\/ before api level 23\r\nint hours =simpleTimePicker.getHour(); \/\/ after api level 23\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. getCurrentMinute():<\/strong><\/span><\/p>\n<p>This method is used to get the current minutes from a time picker.<\/p>\n<p><strong>getMinute():<\/strong><em>\u00a0getCurrentMinute()\u00a0method was deprecated in API level 23. From api level 23 we\u00a0have to use <strong>getMinute(). <\/strong><\/em>This method returns\u00a0 an integer value.<\/p>\n<p>Below we get the value of minutes from a time picker.<\/p>\n<pre>TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); \/\/ initiate a time picker\r\nint minutes = simpleTimePicker.getCurrentMinute(); \/\/ before api level 23\r\nint minutes = simpleTimePicker.getMinute(); \/\/ after api level 23\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. setIs24HourView(Boolean is24HourView):<\/strong><\/span><\/p>\n<p>This method is used to set the mode of the Time picker either 24 hour mode or AM\/PM mode. In this method we\u00a0set a Boolean value either true or false. True value indicate 24 hour mode and false value indicate AM\/PM mode.<\/p>\n<p>Below we set the current mode of the time picker.<\/p>\n<pre>TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); \/\/ initiate a time picker\r\nsimpleTimePicker.setIs24HourView(true); \/\/ set 24 hours mode for the time picker\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1140\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/setIs24HourView-24-hour-am-pm-mode-timepicker.jpg\" alt=\"setIs24HourView 24 hour am pm mode timepicker\" width=\"422\" height=\"339\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/setIs24HourView-24-hour-am-pm-mode-timepicker.jpg 422w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/setIs24HourView-24-hour-am-pm-mode-timepicker-300x241.jpg 300w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/center><span style=\"color: #008000;\"><strong>6. is24HourView():<\/strong><\/span><\/p>\n<p>This method is used to check the current mode of the time picker. This method returns true if its 24 hour mode or false if AM\/PM mode is set.<\/p>\n<p>Below we get the current mode of the time picker:<\/p>\n<pre>TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.simpleTimePicker); \/\/ initiate a time picker\r\nBoolean mode=simpleTimePicker.is24HourView(); \/\/ check the current mode of the time picker\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener):<\/strong><\/span><\/p>\n<p>This method is used to set the callback that indicates the time has been adjusted by the user. onTimeChanged(TimePicker view, int hourOfDay, int minute) is an override function of this listener in which we have three parameters first is for TimePicker, second for getting hour of the day and last is for getting the minutes after changing the time of the time picker.<\/p>\n<p>Below we show the use of on time changed listener of a time picker.<\/p>\n<pre>TimePicker simpleTimePicker = (TimePicker)findViewById(R.id.<em>simpleTimePicker<\/em>); \/\/ initiate a time picker\r\n\r\nsimpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {\r\n@Override\r\npublic void onTimeChanged(TimePicker view, int hourOfDay, int minute) {\r\n\r\n}\r\n});\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of TimePicker:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss about the attributes that helps us to configure a time picker in your xml file (layout).<\/p>\n<p><strong><span style=\"color: #008000;\">1. id:<\/span>\u00a0<\/strong>id is an attribute used to uniquely identify a time picker.<\/p>\n<pre>&lt;TimePicker\r\nandroid:id=\"@+id\/simpleTimePicker\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\/&gt; &lt;!-- id of a time picker --&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. timePickerMode: <\/strong><\/span>time picker mode is an attribute of time picker used to set the mode either spinner or clock. Default mode is clock but this mode is no longer used after api level 21, so from api level 21 you have to set the mode to spinner.<\/p>\n<p>Below we set the mode to spinner.<\/p>\n<pre>&lt;TimePicker\r\nandroid:id=\"@+id\/simpleTimePicker\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:timePickerMode=\"spinner\" \/&gt; &lt;!-- time picker mode of a time picker --&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1142\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/timePickerMode-in-Android.jpg\" alt=\"timePickerMode in Android\" width=\"417\" height=\"286\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/timePickerMode-in-Android.jpg 417w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/timePickerMode-in-Android-300x206.jpg 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/center><span style=\"color: #008000;\"><strong>3. background:<\/strong><\/span> background attribute is used to set the background of a time picker. We can set a color or a drawable image in the background. We can also set the background color programmatically means in java class.<\/p>\n<p>Below we set the orange color for the background of a time picker.<\/p>\n<pre>&lt;TimePicker\r\n    android:id=\"@+id\/simpleTimePicker\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:timePickerMode=\"spinner\"\r\n    android:background=\"#F88F00\" \/&gt; &lt;!-- orange background color for the time picker --&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1143\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/Background-in-Timepicker-Android.jpg\" alt=\"Background in Timepicker Android\" width=\"230\" height=\"258\" \/><\/center><strong>Setting TimePicker Background In Java Class:<\/strong><\/p>\n<pre>TimePicker simpleTimePicker=(TimePicker)findViewById(R.id.simpleTimePicker); \/\/initiate a time picker\r\nsimpleTimePicker.setBackgroundColor(Color.YELLOW); \/\/Yellow background color for the background of a time picker<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. padding:<\/strong><\/span> padding attribute is used to set the padding from left, right, top or bottom for a time picker.<\/p>\n<ul>\n<li><strong>paddingRight:<\/strong> set the padding from the right side of the time picker<strong>.<\/strong><\/li>\n<li><strong>paddingLeft:<\/strong> set the padding from the left side of the time picker<strong>.<\/strong><\/li>\n<li><strong>paddingTop:<\/strong> set the padding from the top side of the time picker<strong>.<\/strong><\/li>\n<li><strong>paddingBottom:<\/strong> set the padding from the bottom side of the time picker<strong>.<\/strong><\/li>\n<li><strong>Padding:<\/strong> set the padding from the all side\u2019s of the time picker<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below example we set the 20dp padding from all the side\u2019s of the time picker.<\/p>\n<pre>&lt;TimePicker\r\nandroid:id=\"@+id\/simpleTimePicker\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:timePickerMode=\"spinner\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_marginTop=\"50dp\"\r\nandroid:padding=\"20dp\"\/&gt; &lt;!-- 20dp padding from all the sides of a time picker --&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1144\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/padding-in-Timepicker.jpg\" alt=\"padding in Timepicker\" width=\"214\" height=\"266\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example of TimePicker in Android Studio:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> In the below example of time picker we will show you the use of time picker in our application. For that we display simple time picker and a textview in our xml file and perform setOnTimeChangedListener() event, so that whenever a user adjust the time the current displayed time of time picker is displayed by using a Toast and also displayed in the textview. Below is the final output, download project code and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/TimePickerExample\" target=\"_blank\" rel=\"nofollow\">Download Project Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1146\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-Example-in-Android-Studio.jpg\" alt=\"TimePicker Example in Android Studio\" width=\"227\" height=\"391\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-Example-in-Android-Studio.jpg 227w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-Example-in-Android-Studio-174x300.jpg 174w\" sizes=\"auto, (max-width: 227px) 100vw, 227px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>TimePickerExample<\/strong><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt; <strong>activity_main.xml (or) main.xml<\/strong> and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying a time picker with spinner mode and textview for displaying time of time picker.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;TimePicker\r\n        android:id=\"@+id\/simpleTimePicker\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"50dp\"\r\n        android:background=\"#090\"\r\n        android:padding=\"20dp\"\r\n        android:timePickerMode=\"spinner\" \/&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/time\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:text=\"Time Is ::\"\r\n        android:textColor=\"#090\"\r\n        android:textSize=\"20sp\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open\u00a0 app -&gt; package -&gt;\u00a0<strong>MainActivity.<\/strong><strong>java<\/strong><\/p>\n<p>In this step we open MainActivity where we add the code to initiate the time picker and a text view to display time of time picker and then we perform setOnTimeChangedListener() event so whenever a user adjust the time the current displayed time of time picker is displayed by using a Toast and also displayed in the textview.<\/p>\n<pre>package example.gb.timepickerexample;\r\n\r\nimport android.app.TimePickerDialog;\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.TextView;\r\nimport android.widget.TimePicker;\r\nimport android.widget.Toast;\r\n\r\nimport org.w3c.dom.Text;\r\n\r\nimport java.util.Calendar;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    TextView time;\r\n    TimePicker simpleTimePicker;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/  initiate the view's\r\n        time = (TextView) findViewById(R.id.time);\r\n        simpleTimePicker = (TimePicker) findViewById(R.id.simpleTimePicker);\r\n        simpleTimePicker.setIs24HourView(false); \/\/ used to display AM\/PM mode\r\n        \/\/ perform set on time changed listener event\r\n        simpleTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {\r\n            @Override\r\n            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {\r\n                \/\/ display a toast with changed values of time picker\r\n                Toast.makeText(getApplicationContext(), hourOfDay + \"  \" + minute, Toast.LENGTH_SHORT).show();\r\n                time.setText(\"Time is :: \" + hourOfDay + \" : \" + minute); \/\/ set the current time in text view\r\n            }\r\n        });\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the App in AVD and you will see TimePicker on the screen. Change the time and it will be displayed as Toast and also in TextView.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1146\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-Example-in-Android-Studio-174x300.jpg\" alt=\"TimePicker Example Output\" width=\"174\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-Example-in-Android-Studio-174x300.jpg 174w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePicker-Example-in-Android-Studio.jpg 227w\" sizes=\"auto, (max-width: 174px) 100vw, 174px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example of TimePickerDialog in Android Studio:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 2:<\/strong><\/span> In the second example of TimePicker we will show the use of time picker dialog in our application. To do that we will display edittext in our xml file and perform a click listener event on it, so whenever a user click on it time picker dialog will appear and from there user can adjust the time and after selecting the time it will be displayed in the edittext.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/TimePickerExample\" target=\"_blank\" rel=\"nofollow\">Download Project Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1147\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/TimePickerDialog-Example-in-Android-Studio.jpg\" alt=\"TimePickerDialog Example in Android Studio\" width=\"507\" height=\"249\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePickerDialog-Example-in-Android-Studio.jpg 507w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePickerDialog-Example-in-Android-Studio-300x147.jpg 300w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>TimePickerExample<\/strong><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt; <strong>activity_main.xml (or) main.xml<\/strong> and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying a edittext to display time of time picker.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/time\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:hint=\"Select Time...\"\r\n        android:textColor=\"#090\"\r\n        android:textColorHint=\"#090\"\r\n        android:background=\"#d4d4d4\"\r\n        android:padding=\"15dp\"\r\n        android:textSize=\"20sp\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open src -&gt; package -&gt;\u00a0<strong>MainAcivity.<\/strong><strong>java<\/strong><\/p>\n<p>In this step we open MainActivity where we add the code to initiate the edittext to display time of time picker and perform click event on edittext, so whenever a user clicks on edittext a time picker dialog will appear from there user can set the time. And finally then the time will be displayed in the edit text.<\/p>\n<pre>package example.gb.timepickerexample;\r\n\r\nimport android.app.TimePickerDialog;\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.EditText;\r\nimport android.widget.TextView;\r\nimport android.widget.TimePicker;\r\nimport android.widget.Toast;\r\n\r\nimport org.w3c.dom.Text;\r\n\r\nimport java.util.Calendar;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    EditText time;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/  initiate the edit text\r\n        time = (EditText) findViewById(R.id.time);\r\n        \/\/ perform click event listener on edit text\r\n        time.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                Calendar mcurrentTime = Calendar.getInstance();\r\n                int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);\r\n                int minute = mcurrentTime.get(Calendar.MINUTE);\r\n                TimePickerDialog mTimePicker;\r\n                mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {\r\n                            @Override\r\n                            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {\r\n                                time.setText(selectedHour + \":\" + selectedMinute);\r\n                    }\r\n                }, hour, minute, true);\/\/Yes 24 hour time\r\n                mTimePicker.setTitle(\"Select Time\");\r\n                mTimePicker.show();\r\n\r\n            }\r\n        });\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><strong>\u00a0Output:<\/strong><\/p>\n<p>Now run the App in AVD and you will see edittext asking user to select time. When user click on it, timepicker dialog will open from there user can select time. And then this time will be displayed in EditText.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1147 size-medium\" src=\"\/ui\/wp-content\/uploads\/2016\/03\/TimePickerDialog-Example-in-Android-Studio-300x147.jpg\" alt=\"TimePickerDialog Example Output\" width=\"300\" height=\"147\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePickerDialog-Example-in-Android-Studio-300x147.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/03\/TimePickerDialog-Example-in-Android-Studio.jpg 507w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, TimePicker is a widget used for selecting the time of the day in either AM\/PM mode or 24 hours mode. The displayed time consist of hours, minutes and clock format. \u00a0If we need to show this view as a Dialog then we have to use a TimePickerDialog class. TimePicker code: &lt;TimePicker android:id=&#8221;@+id\/simpleTimePicker&#8221; android:layout_width=&#8221;wrap_content&#8221; &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/timepicker\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">TimePicker Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1096","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>TimePicker Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn how TimePicker is used for selecting Time in Android with example. TimePicker is a widget used for selecting the time of the day in either AM\/PM mode or 24 hours mode.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/timepicker\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1096"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1096\/revisions"}],"predecessor-version":[{"id":2804,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1096\/revisions\/2804"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}