Android TimePicker (Wheel Style) not responding correctly to flick gestures inside ScrollView
Asked Answered
C

2

17

I have a dialog box that contains a Scrollview, which contains a layout with two TimePickers.

The timepickers are the newer style ones, what's in ICS.

The problem is that they seem to fight for focus when you change the time by dragging the wheel, or flicking it. It will change the time just a little, and then the layout will scroll instead.

Any ideas? Thanks in advance.

Croatia answered 13/1, 2012 at 7:20 Comment(0)
D
46

I had the same problem when using the Holo theme, and here is where I found the solution: https://groups.google.com/forum/?fromgroups#!topic/android-developers/FkSfJI6dH8w

You must implement your custom DatePicker or TimePicker and override the following method:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
    {
        ViewParent p = getParent();
        if (p != null)
            p.requestDisallowInterceptTouchEvent(true);
    }

    return false;
}
Desiderate answered 13/3, 2012 at 15:57 Comment(2)
You are a champ man, I tried so many things, implementing on touch listener wasn't working, but this worked.Bodi
For anyone who uses this, be sure to also define the correct constructor: public CustomDatePicker(Context context, AttributeSet attrs){ super(context, attrs);}Sf
D
3

Because the link from Klemens Zleptnig is broken, here is a complete example. This fix helps with the scroll of a TabLayout too btw. I excluded the area around the big numbers in the top of the TimePicker because they don't need the scroll event anyway.

xml:

<com.name.app.MyTimePicker
                android:id="@+id/timePicker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
.../>

java:

public class MyTimePicker extends TimePicker {
    public MyTimePicker(Context context) {
        super(context);
    }

    //This is the important constructor
    public MyTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        //Excluding the top of the view
            if(ev.getY() < getHeight()/3.3F)
                return false;

            ViewParent p = getParent();
            if (p != null)
                p.requestDisallowInterceptTouchEvent(true);
        }

        return false;
    }
}
Debbiedebbra answered 18/3, 2020 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.