Pergunta

I have used the code on this Android developers page to create a time picker when a button is pressed. However, I'm not getting the right look for it. I'm targeting my app to API 18 and the minimum is 10, because I want to support 2.3 due to the high usage. The minimum required SDK doesn't seem to have effect on the problem (I set it to 16):

I'm debugging the application on my HTC One S with Android 4.1.2, API 16. I get this kind of time picker but I want it to look modern, like in the Android developers page linked earlier.

http://www.b2creativedesigns.com/Tutorials/TimePicker/tutorial_timepicker2.png

Why does it use this prehistoric looking time picker?

I create the dialog using the following snippet. I call it from a FragmentActivity.

DialogFragment newFragment = TimePickerFragment.newInstance(2);
newFragment.show(getSupportFragmentManager(), "endtime");

This is the code for TimePickerFragment.

public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
    private static int id;

    public static TimePickerFragment newInstance(int _id) {
        Bundle args = new Bundle();
        args.putInt("id", _id);

        id = _id;

        TimePickerFragment f = new TimePickerFragment();
        f.setArguments(args);

        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        id = getArguments().getInt("id");

        int hour = start / 60;  // start is a global variable, no problems in these
        int minute = start % 60;

        if(id == 2) {
            hour = end / 60;
            minute = end % 60;
        }

        return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        System.out.println(hourOfDay + ":" + minute);
        setTime(id, hourOfDay, minute);
    }

}
Foi útil?

Solução

Use ContextThemeWrapper to explicitly provide your Holo-based theme for the Dialog, e.g.

return new TimePickerDialog(
        new ContextThemeWrapper(getActivity(), R.style.YourAppStyleInheritingFromHolo),
        this, hour, minute, DateFormat.is24HourFormat(getActivity()));

Outras dicas

You need to use the Holo theme for the Holo widgets to work in your app. For example :

in your Manifest, under application :

android:theme="@style/AppTheme"

and in your res/values folder :

<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">
</style>

while using this in values-v11 and values-v14 :

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar">
</style>

The system will switch between Theme.Light and Theme.Holo.Light depending on the Android version on the device.

You might want to take a look at this library. It is a backported TimePicker from Android 4.2 which works from android 2.1 upwards.

https://github.com/SimonVT/android-timepicker

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top