6

This is may layout xml:

<RelativeLayout
    android:id="@+id/seek_bar_wrapper"
    android:layout_width="match_parent"
    android:layout_height="200dip">

    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="20dip"
        android:maxHeight="2dip"
        android:minHeight="2dip"
        android:progressDrawable="@drawable/drawable_seekbar_progress"
        android:thumb="@drawable/drawable_seekbar_thumb" />
</RelativeLayout>

I set an OnClickListener to the RelativeLayout. The strange thing is that when I touch the RelativeLayout, the SeekBar performs like I press it, it's thumb become the drawable when it's state is pressed. I think it's state become pressed.

Is this a bug and how to deal with it?

L. Swifter
  • 3,179
  • 28
  • 52
  • I see you've made 3 other questions since I posted this answer. If this helped you please accept the answer so we can both get rep, if not let me know where you're having trouble. – Dave S Apr 27 '16 at 18:32

2 Answers2

15

Just set

 android:clickable="true"
 android:focusable="true"

To your seekbar

Vadim
  • 3,855
  • 2
  • 17
  • 22
3

ViewGroups seem to pass their state down to their children when they are clickable as seen in this question: View gets its drawable state (pressed, etc...) from its parent

This may be happening because you set the OnClickListener to the RelativeLayout.

My first inclination would be to put a Button behind the SeekBar as a sibling to the SeekBar, and using that for your OnClick action.

You could also override your RelativeLayout, and do nothing on setPressed

Android: Child elements sharing pressed state with their parent even when duplicateParentState specified

According to the docs: http://developer.android.com/training/gestures/viewgroup.html

Handling touch events in a ViewGroup takes special care, because it's common for a ViewGroup to have children that are targets for different touch events than the ViewGroup itself. To make sure that each view correctly receives the touch events intended for it, override the onInterceptTouchEvent() method.

So it seems this is intended behavior. If you want to avoid this behavior it looks like you will need to override the ViewGroup (RelativeLayout) or use a View that is not a parent to the SeekBar for this event.

Community
  • 1
  • 1
Dave S
  • 3,378
  • 1
  • 20
  • 34