How to use xml timepicker?

I have a dialog button and timepicker created in the same xml code below:

<?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/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent"> <Button android:layout_height="wrap_content" android:id="@+id/dialog_ok" android:text="OK" android:layout_width="fill_parent" android:layout_weight="1"></Button> <Button android:layout_height="wrap_content" android:text="Cancel" android:id="@+id/dialog_cancel" android:layout_width="fill_parent" android:layout_weight="1"></Button> </LinearLayout> </LinearLayout> 

And then I created a dialog in java code, and also bring in a dialog box and timepicker from xml code, as shown below:

 public void TimePickerDialog(){ final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.sign_in_dialog); /////////////////////updated code//////////////////// TimePicker timePicker1 = (TimePicker) findViewById(R.id.timePicker1); timePicker1.setIs24HourView(true); ///////////////////////////////// dialog.setTitle("Quick Mute"); //Allow the dialog to be cancelable dialog.setCancelable(true); // Here we add functionality to our dialog box content. In this example it the two buttons //reference the button Button okButton = (Button) dialog.findViewById(R.id.dialog_ok); //Create a click listener okButton.setOnClickListener(new OnClickListener() { //override the onClick function to do something public void onClick(View v) { } }); //reference the button Button cancelButton = (Button) dialog.findViewById(R.id.dialog_cancel); //Create a click listener cancelButton.setOnClickListener(new OnClickListener() { //override the onClick function to do something public void onClick(View v) { //Close the dialog</span> dialog.dismiss(); } }); //finally show the dialog dialog.show(); } 

I have a problem, how can I access the timepicker that was created in the xml code so that I can make the timepicker a 24-hour timepicker? ... as well as how can I get the timepicker that is in the xml code?

+4
source share
3 answers

As usual, this is done:

 TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker1); 

and, as usual, this only works after calling dialog.setContentView . Then, to get the selected time, call:

 timePicker.getCurrentHour(); timePicker.getCurrentMinute(); 
+3
source

You cannot set 24-hour mode in XML, use MyTimePicker.setIs24HourView(boolean) instead.

See this example of how to get time.

+1
source

These are the steps to get a TimePicker instance:

  final Dialog mTimeDialog = new Dialog(this); final RelativeLayout mTimeDialogView = (RelativeLayout)getLayoutInflater().inflate(R.layout.time_dialog, null); final TimePicker mTimePicker = (TimePicker) mTimeDialogView.findViewById(R.id.TimePicker); mTimePicker.setIs24HourView(true); 
0
source

All Articles