To provide a widget for selecting a time, use the TimePicker widget, which allows the user to select the hour and minute in a familiar interface.
In this article, I am going to explain how to create a TimePickerDialog, which presents the time picker in a floating dialog box at the press of a button. When the time is set by the user, a TextView will update with the new date.
· Start a new project named TimePickerDemo.
· Open the res/layout/main.xml file and insert the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/timeDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/hello"/>
<Button
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Change the time"/>
</LinearLayout>
Open the activity file and add the following code as shown below:
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class TimePickerActivity extends Activity {
private TextView mTimeDisplay;
private Button mPickTime;
private int mHour;
private int mMinute;
static final int TIME_DIALOG_ID=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// capture our View elements
mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
mPickTime=(Button)findViewById(R.id.timePicker);
// add a click listener to the button
mPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
// get the current time
final Calendar c=Calendar.getInstance();
mHour=c.get(Calendar.HOUR_OF_DAY);
mMinute=c.get(Calendar.MINUTE);
// display the current date
updateDisplay();
}
// updates the time we display in the TextView
private void updateDisplay()
{
mTimeDisplay.setText(new
StringBuilder().append(pad(
StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)));
}
private Object pad(int mMinute2) {
if(mMinute2>=10)
return String.valueOf(mMinute2);
else
return "0"+String.valueOf(mMinute2);
}
// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mtimeSetListener=new
TimePickerDialog.OnTimeSetListener() {
TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour=hourOfDay;
mMinute=minute;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case TIME_DIALOG_ID:
return new TimePickerDialog(this,mtimeSetListener,mHour,mMinute,false);
}
return null;
}
}
· Run the application.
Output will something like below:
When you click on the button (Change the time), a time picker dialog will open, you can set the time from the time picker dialog.
After setting the time, a new time will display in the TextView.
Chris Anderson
02-May-2012If you are facing problem in retrieve and display information from SQLite database, you can paste your code here.
Thanks.
Ngang Sir Tat
01-May-2012Chris Anderson
01-May-2012Here in TimePicker and DatePicker, I am not using SQLite Database.
If you want to retrieve and display information from SQLite Database, check this links:
http://www.mindstick.com/Articles/af5c031a-e435-4642-8464-9f7d375087c2/?SQLite%20in%20Android
http://www.mindstick.com/Articles/2643e627-4f5c-45cc-b166-9a9b4c9ff3b1/?Select%20query%20in%20Android%20Application
Thanks.
Ngang Sir Tat
01-May-2012