Kornelije Petak
Kornelije Petak

Reputation: 9572

Programmatically switch TimePicker back to the hour setting mode

I have created a class that inherits from Android.Widget.TimePicker.

For reasons of design, the header that contains the textual representation is hidden.

When I run the app, and the user selects the hour, the picker switches to selecting minutes. Once it's there, additional taps on the clock will just change the minutes selection.

However, since the header had to be removed, user is unable to click the HOUR part of the time so there's no way for the clock to return to selecting hours again.

Question: Is there a programmatic way to return the picker to the hour selection again?

Upvotes: 0

Views: 576

Answers (2)

rydzu
rydzu

Reputation: 1

If anyone still need solution for this:

In this solution we need header... but we can set it's visibility to GONE.

All we need to do is programmatically perform click on header's hour.

ViewGroup viewGroup = (ViewGroup) this.getChildAt(0);
ViewGroup header = viewgroup.getChildAt(0);
View hourElement = header.getChildAt(0);
hourElement.performClick();

this.getChildAt(0);

I used this as a method in my custom TimePicker Class, but probably in "normal" class timepicker.getChildAt(0) should work too;

Upvotes: 0

Deˣ
Deˣ

Reputation: 4371

Try this. With these methods you can choose and show minute or hourview.

 private void setMinuteView() {
        try {
            Method m = TimePicker.class.getDeclaredMethod("getMinuteView");
            m.setAccessible(true);
            View view = (View) m.invoke(simpleTimePicker);
            view.performClick();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    private void setHourView() {
        try {
            Method m = TimePicker.class.getDeclaredMethod("getHourView");
            m.setAccessible(true);
            View view = (View) m.invoke(simpleTimePicker);
            view.performClick();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

Related Questions