Hello peeps! Wondering about how to add a time picker in the Android app?
You are at the correct spot!
Omninos Solutions have been doing exceptional work when it comes to Android App development, be it creating apps, app clones, or adding features to the app. We have a team of highly talented professionals who have successfully dealt with 500+ projects. The specialists are carrying out business-centric work, which is a vital characteristic of OMNINOS. With the developing trends, numerous features are being added to Android Apps. Time Picker is one of the very significant features of Androids which can be successfully installed in the android app.
What is an Android Time Picker?
"Android Time Picker", the term that we have been gloating for, let's see what it is about.
Time Picker is described as a widget in Android which is used for specifying the time of the day. The time can be either in AM/ PM more or in the 24-hour mode. The time picker gives control for specifying all the parts of time (hours, minutes, AM/PM) except seconds and of date month, day, year). If a time picker is installed in the android application to ensure the users select a valid time for the day.
There can be a clock format or time picker dialogue. The android date and time picker can be termed as a custom time picker as the users are provided with the privilege to customize the time accordingly.
Here, for your convenience, I have displayed the pictorial representation of the different android time pickers.
I hope now as we know what a time picker is, we are all set to explore how to install the android time duration picker.
So without doing much ado, let's get started!
The Pre-work
Introducing time picker android is similar to creating art. As all the work needs to take place systematically. So, before the creation of our art, let's get our canvas and brushes ready.
- The very first prep that you need to do is, define a TimePicker component in activity.xml, as shown below.
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- Once the object for TimePicker is created get a reference to the xml component.
import android.widget.TimePicker;
private TimePicker timePicker1;
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
- You will be using getCurrentHour() and getCurrentMinute() method in the android time duration picker. The syntax is as follows,
int hour = timePicker1.getCurrentHour();
int min = timePicker1.getCurrentMinute();
Methods & Description
Apart from the methods mentioned above, there are other methods from which the control over the android time picker can be increased. There are as follows,
- setCurrentHour(Integer currentHour) - This method helps to set the current hour.
- setCurrentMinute(Integer currentMinute) - This method helps to set the current minute.
- is24HourView() - This method is true in the case of 24-hour view otherwise false.
- setIs24HourView(Boolean is24HourView) - It sets the mode whether it is 24 hour or AM/ PM mode.
- isEnabled() - This method is for returning the enabled status of the view.
- setEnabled(boolean enabled) - It enables the state of view.
- setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener) - This method is for setting the callback, which is an indicator that the time has been modified by the user.
Steps to use Time Picker in Android App
I'll now be presenting an example for a detailed description of creating an android time picker application.
- Create a new project in Android Studio. Don't forget to give a reasonable title.
- The targeted Android devices are then selected.
- Add a suitable activity in mobile, which will be an Empty Activity.
- Perform final customization.
The images are just for reference.
The following is the content of the java, "MainActivity"
package com.example.timepicker;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TimePicker timePicker1;
private TextView time;
private Calendar calendar;
private String format = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
time = (TextView) findViewById(R.id.textView1);
calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int min = calendar.get(Calendar.MINUTE);
showTime(hour, min);
}
public void setTime(View view) {
int hour = timePicker1.getCurrentHour();
int min = timePicker1.getCurrentMinute();
showTime(hour, min);
}
public void showTime(int hour, int min) {
if (hour == 0) {
hour += 12;
format = "AM";
} else if (hour == 12) {
format = "PM";
} else if (hour > 12) {
hour -= 12;
format = "PM";
} else {
format = "AM";
}
time.setText(new StringBuilder().append(hour).append(" : ").append(min)
.append(" ").append(format));
}
}
- The content of xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/time_pick"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/set_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="180dp"
android:onClick="setTime"
android:text="@string/time_save" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/set_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_alignTop="@+id/set_button"
android:layout_marginTop="67dp"
android:text="@string/time_current"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/time_selected"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
- Content of xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TimePicker</string>
<string name="action_settings">Settings</string>
<string name="time_picker_example">Time Picker Example</string>
<string name="time_pick">Pick the time and press the save button</string>
<string name="time_save">Save</string>
<string name="time_selected"></string>
<string name="time_current">The Time is:</string>
</resources>
And we are done!
Conclusion
Try and run the TimePicker application. You can also easily inculcate Android tabs in the time picker android application. If your app is a bit complicated you may have to hunt a bit harder or you can consult Omninos Solutions for proper guidance. I would always suggest consulting experts if the app is complex as the TimePickerDialog integration becomes problematic. This was a complete guide regarding android time picker.
Hope all your questions, regarding the android time duration picker, are resolved. For further queries, you can always consult Omninos Solution.