Erol Asan
Erol Asan

Reputation: 388

Android time picker shows two clocks?

I have implemented the Android time picker according to the android dev doc, and everything works fine, but I am having a strange bug with it. I searched for it a lot and didn't find anything so, I am asking here.

Here is my time picker:
timepicker picture - to see the bug

Here is the code for it:

 class TimePickerFragment extends DialogFragment
            implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            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
            if (hourOfDay < 10) {
                startTime.setText("0" + Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
            } else if (minute < 10) {
                startTime.setText(Integer.toString(hourOfDay) + ":0" + Integer.toString(minute));
            } else if (hourOfDay < 10 && minute < 10) {
                startTime.setText("0" + Integer.toString(hourOfDay) + ":0" + Integer.toString(minute));
            } else if (minute == 0) {
                startTime.setText(Integer.toString(hourOfDay) + ":00");
            } else
                startTime.setText(Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
        }
    }

Does anyone knows how to fix this?

Upvotes: 2

Views: 439

Answers (2)

redlabrat
redlabrat

Reputation: 507

I faced with the same issue on Samsung devices (Galaxy S5 and S6 in my case). I found that on this devices title is added by system and to hide that title all you need to do is to set it to null by your own. Hierarchy view of broken TimePickerDialog in my case

TimePickerDialog dialog = new TimePickerDialog(getActivity(), this, hour, minute, false);
dialog.setTitle(null);

Upvotes: 1

Mahmoud Ibrahim
Mahmoud Ibrahim

Reputation: 1085

TimePickerDialog (Context context, 
            int themeResId, 
            TimePickerDialog.OnTimeSetListener listener, 
            int hourOfDay, 
            int minute, 
            boolean is24HourView)

is24HourView : Whether this is a 24 hour view, or AM/PM.

try to set this parameter to false, I think you will get what you want.

Reference: https://developer.android.com/reference/android/app/TimePickerDialog.html

Upvotes: 0

Related Questions