TimePicker can be defined as below –
"Android TimePicker is used to select date. It allows you to choose time by hour and minute. You can't select time by seconds."
Attributes of android TimePicker are –
S. No. | XML Attributes | Description |
---|---|---|
1 | android:timePickerMode | Specifies look of the TimePicker. |
2 | android:id | This is unique id of the TimePicker to uniquely identify the TimePicker. |
3 | android:height | Height of the TimePicker. |
4 | android:width | Width of the TimePicker. |
5 | android:background | Sets background of the view. |
6 | android:clickable | Sets whether view is clickable or not. |
7 | android:fadeScrollbars | Defines whether scrollbar should fade out when not in use or not. |
8 | android:fitsSystemWindows | Defines whether to adjust view layout according to system windows. |
9 | android:minHeight | Defines minimum height. |
10 | android:minWidth | Defines minimum width. |
11 | android:padding | Defines padding of the view. |
12 | android:paddingBottom | Defines padding to bottom of the view.. |
13 | android:paddingEnd | Defines padding to end of the view. |
14 | android:visibility | Defines visibility of the view. |
Some of the popular attributes of TimePicker inherited from FrameLayout are –
S. No. | XML Attributes | Description |
---|---|---|
1 | android:foregroundGravity | It defines the gravity of the foreground drawable. |
2 | .android:measureAllChildren | When measuring, It specifies whether to measure all children or only those that are in visible or invisible state. |
Some of the popular TimePicker attributes inherited from ViewGroup are –
S. No. | XML Attributes | Description |
---|---|---|
1 | android:animationCache | Defines whether layout animations should create a drawing cache for their children. |
2 | android:layoutAnimation | Defines the layout animation to use the first time the ViewGroup is laid out. |
3 | android:layoutMode | Defines the layout mode. |
At first, we will create android application. Then, we will use TimePicker in the application.
Follow steps below to create new project. Please ignore the steps if you've already created a new application.
S. No. | Steps |
1 | Open Android Studio. |
2 | Click on Start a new Android Studio Project Write application name as TimePicker. Then, click next button. |
3 | Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button. |
4 | Then, select Empty Activity => click next => click finish. |
5 | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail. |
Now, we will modify xml and java file to use TimePicker in the application.
Open res/values/strings.xml file. Then, add below code into it.
<resources>
<string name="app_name">TimePicker</string>
<string name="selected_time">The selected time is:</string>
</resources>
Open res/layout/activity_main.xml file. Then, add below code into it.
<?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"
android:gravity="center"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginLeft="35dp"
android:layout_marginTop="30dp" />
</LinearLayout>
In activity_main.xml file, we've defined TimePicker and TextView. TextView will be used to display the selected time in TimePicker.
Now, we will access these timePicker and TextView in java file to perform some actions on it.
Open src/main/java/com.ukacademe.timepicker/MainActivity.java file. Then, add below code into it.
package com.ukacademe.timepicker;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSelectedTime();
}
private void getSelectedTime() {
final TextView textView = findViewById(R.id.textView);
TimePicker timePicker = findViewById(R.id.timePicker);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
String format = "";
if (hourOfDay == 0) {
hourOfDay += 12;
format = "AM";
} else if (hourOfDay == 12) {
format = "PM";
} else if (hourOfDay > 12) {
hourOfDay -= 12;
format = "PM";
} else {
format = "AM";
}
if (textView != null) {
String hour = String.valueOf(hourOfDay < 10 ? "0" + hourOfDay : hourOfDay);
String min = String.valueOf(minute < 10 ? "0" + minute : minute);
String text = getString(R.string.selected_time) + " " + hour + " : " + min + " " + format;
textView.setText(text);
textView.setVisibility(View.VISIBLE);
}
}
});
}
}
In MainActivity.java file, we've accessed TextView and TimePicker. Then, we have set a listener, time change listener, to get the time selected in TimePicker. Then, we have shown the selected time in TextView after some calculations. Calculation is just to show the time in proper format.
Since the file of AndroidManifest.xml is very important in any android application, we are also going to see the content inside this file.
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.timepicker">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When we run the program, we will get output as shown below.