Reputation: 5362
I want to know if there is a way to get the time that the user has set in a TimePicker on the screen without using a Dialog. I don't see the point in creating a button that will pop up a TimePickerDialog when I can just have the TimePicker on the screen. I.e. There is no point in having two TimePickers as can be seen in this tutorial and many other similar TimePicker tutorials: http://www.mkyong.com/android/android-time-picker-example/
I am using picker.getCurrentHour() and picker.getCurrentMinute() but it just returns me the system's current time after I press a button. Is there no way to get the time that the user has entered?
This is how I retrieve the time after clicking a button:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_time);
picker = new TimePicker(SetTimeActivity.this);
Button save=(Button)findViewById(R.id.saveAlarmButton);
save.setOnClickListener(onSave);
}
private View.OnClickListener onSave=new View.OnClickListener()
{
public void onClick(View v)
{
hour=picker.getCurrentHour();
min=picker.getCurrentMinute();
System.out.println(hour + " : " + min);
}
};
<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=".SetTimeActivity" >
<Button
android:id="@+id/saveAlarmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Save" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Set time for alarm"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
But it just gives me the current time.
Upvotes: 0
Views: 1659
Reputation: 6563
Your code should work properly. Probably the issue appear when you are trying to input date through number input. It is due to NumberPicker
update, it is done ones picker lose focus. So you can try to make sure that picker lose its focus, so it will consume current user input:
picker.clearFocus();
hour=picker.getCurrentHour();
min=picker.getCurrentMinute();
Upvotes: 1
Reputation: 116040
The definition of the picker classes is that it opens a dialog to pick something:
Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs.
Of course, you can always try out other solutions that provide you custom pickers and get their layout, for example this one , here , here and here.
This way, you can put the layout of the picker wherever you wish, without any dialog.
Upvotes: 0
Reputation: 179
You can add the widget to an activity just like any other. You can get the date/time like so:
// Format date and time from pickers
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Upvotes: 0