9

Hopefully there is a solution here, I have a XML TimePicker and ScrollView in my main.xml and with the set up the TimePicker does not scroll. If I remove the ScrollView the Timepicker will scroll just fine but obviously I need both.

It seems like they are both fighting to get the touch events but it's causing problems.

Here is my code:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/backgroundmain">

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Action Title -->
        <ImageView />

        <TextView />

        <!-- Description -->
        <TextView />

        <!-- Have to Button -->
        <TextView />

        <RelativeLayout
            android:id="@+id/TimePickerSection"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/">

            <TimePicker 
                android:id="@+id/TimePick"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_centerVertical="true"
                android:layout_below="@id/HaveToText"/>

            <Button
                android:id="@+id/OkButton"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/OkText"
                android:layout_below="@id/HaveToText"
                android:textSize="20sp"
                android:onClick="TimePickButton"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/TimePick"/>

        </RelativeLayout>

    <!-- OR -->
    <TextView />

    <!-- buttons -->
    <TextView />

    <Button />

    </RelativeLayout>

</ScrollView>

I've removed the irrelevant code, but kept the outlines just to show you how the layout is laid out.

Is there a fix in the XML? I've tried things like android:fillViewPort="" but that has done nothing. My TimePicker isn't part of a Dialog or anything and hopefully I can keep it that way.

JJD
  • 50,076
  • 60
  • 203
  • 339
RED_
  • 2,997
  • 4
  • 40
  • 59

1 Answers1

15

Found the solution, create a custom TimePicker class and then call this to override touch events:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Stop ScrollView from getting involved once you interact with the View
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        ViewParent p = getParent();
        if (p != null)
            p.requestDisallowInterceptTouchEvent(true);
    }
    return false;
}
JJD
  • 50,076
  • 60
  • 203
  • 339
RED_
  • 2,997
  • 4
  • 40
  • 59
  • Don't forget to to override at least the second constructor TimePicker(Context context, AttributeSet attrs) and to use the full path for the custom view in xml – einUsername Mar 18 '20 at 22:32