Shajeel Afzal
Shajeel Afzal

Reputation: 5953

Selector of the parent view is not working when any child is clicked

XML of the selector is:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_enabled="true" android:state_selected="true"/>
    <item android:drawable="@color/primary_light_transparent" android:state_activated="true" android:state_enabled="true"/>
    <item android:drawable="@drawable/normal"/>
</selector>

And the layout XML is:

<FrameLayout
android:id="@+id/keypad_6"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:background="@drawable/button_selector"
     android:descendantFocusability="beforeDescendants"
     android:onClick="onClick" >

     <Button
     android:id="@+id/keypad_6_bt"
     style="@style/Button.Keypad.Numeric"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:onClick="onClick"
     android:text="6" />

    <TextView
     android:id="@+id/keypad_6_tv"
     style="@style/Keypad_Letters"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="bottom"
     android:gravity="center_horizontal"
     android:onClick="onClick"
     android:text="MNO"
     android:textColor="@color/keypad_digits_color" />
</FrameLayout>

What i am looking for is that when the child views (Button or TextView) of the FrameLayout will be clicked the selector of the FrameLayout should work and should highlight the whole layout.

I am unable to achieve this and looking forward to know how can i do this

Upvotes: 2

Views: 1136

Answers (2)

Sami Eltamawy
Sami Eltamawy

Reputation: 10009

When the child is being pressed it takes the focus not the parent. So forget about doing this using a selector

My suggestion for you is: that you have to do it programatically within your activity or fragment by having a reference for the parent layout and change its background programatically according to the state that you listen to from your child. For example

FrameLayout parentLayout = findViewById(R.id.keypad_6);
Button button6 = (Button) findViewById(R.id.keypad_6_bt);
button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
         if (MotionEvent.ACTION_DOWN == event.getAction()){
                parentLayout.setBackgroundColor(Color.BLACK);
            }else if (MotionEvent.ACTION_UP == event.getAction()){
                parentLayout.setBackgroundColor(Color.white);
            }
            return false;
        }
    });

Upvotes: 0

Archie.bpgc
Archie.bpgc

Reputation: 24012

add android:addStatesFromChildren="true" to your FrameLayout

Upvotes: 5

Related Questions