I have a layout where I placed a TimePicker and two buttons for select time:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TimePicker android:id="@+id/time_picker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<Button android:id="@+id/time_picker_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/yes"
android:onClick="set"/>
<Button android:id="@+id/time_picker_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/no"
android:onClick="cancel"/>
</LinearLayout>
</LinearLayout>
I call it through method from another activity:
public void showPicker(View view) {
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.time_picker, null);
dialogView = new TimePicker(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.time_start_label);
builder.setView(dialogView);
ad = builder.create();
ad.show();
}
And I have a handler for “yes” button:
public void set(View view) {
LayoutInflater li = LayoutInflater.from(this);
LinearLayout ll = (LinearLayout) li.inflate(R.layout.time_picker, null);
TimePicker tp = (TimePicker) ll.findViewById(R.id.time_picker);
int hour = tp.getCurrentHour();
int minute = tp.getCurrentMinute();
Utils.showMessage(this, hour + " " + minute); // Toast message
}
But when I click on @+id/time_picker_ok I get a current system time instead of a selected time.
How can I get selected time?
getCurrentXXX() is supposed to answer the current hour or minute.
To read the time entered by the user, you have to use a listener like this:
See this tutorial for more details: http://developer.android.com/resources/tutorials/views/hello-timepicker.html