I’m trying to use the pickers in order the users to be able to select the hour. I create one FragmentActivity with the corresponding android XML. The FragmentActivity is the following:
public class MainActivity extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TimePickerFragment tpf = new TimePickerFragment();
tpf.getFragmentManager();
}
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour,minute,DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment ();
newFragment.show(getFragmentManager(), "timePicker");
}
}
}
The XML is the following:
<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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:onClick="TimePickerFragment " />
</RelativeLayout>
The code was retrieved from: http://developer.android.com/guide/topics/ui/controls/pickers.html
Now, the problem is that I’m not able to see the pickers with the date. It just display a white screen. What am I doing wrong?
P.S. the only change that I have done, id that I use the getFragmentManager() instead of the getSupportFragmentManager(). And this why,when I’m using the last one is hitting an error.
Your
android:onClickattribute isn’t correct. Your xml should be:showTimePickerDialogwill now be called when the user clicks theButton. This method should be inMainActivity.Also, you need to set the layout to be used by calling
setContentView.MainActivitylooks like this (I haven’t includedTimePickerFragment):