MoranAk
MoranAk

Reputation: 211

How to get a press event on a parent layout?

I'm working on Android 2.2.3. I have a Button inside a FrameLayout, so that the button covers the entire FrameLayout.

When i press the button only the button's selector gets the event of pressing. The frame's selector dose not get this event since the button is on top of it. i.e: the attribute "android:state_pressed="true" happens for the button and not for the frame.

i need the press event and not the click event.

I would like to make the frame's selector to be set to android:state_pressed="true" when the button is pressed.

I've tried using

mMuteBtn.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mFrameMute.setPressed(true);
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                mFrameMute.setPressed(false);
            }
            return false;   
        }
    });

but it blocked the onClick() event which I also need for some other work.

My main.xml file is:

 <FrameLayout
    android:id="@+id/frame_mute1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/selector_background" >

    <Button
        android:id="@+id/mute_btn1"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"

        android:background="@android:color/transparent"
        android:drawableTop="@drawable/selector_button"

        android:text="mute"
        android:textColor="@android:color/black"
        android:textSize="12sp" />

</FrameLayout>

The selector_background:

<item android:drawable="@drawable/bg2" android:state_pressed="true"/>
<item android:drawable="@drawable/bg3" android:state_selected="true"/>

<!-- default -->
<item android:drawable="@drawable/bg1"/>

The selector_button:

<item android:drawable="@drawable/image2" android:state_pressed="true"/>
<item android:drawable="@drawable/image3" android:state_selected="true"/>

<!-- default -->
<item android:drawable="@drawable/image1"/>

Upvotes: 1

Views: 880

Answers (1)

Xavi Rigau
Xavi Rigau

Reputation: 1419

Maybe you can start by returning true in the onTouch method in order to keep receiving touch events.

Did you try to set the FrameLayout to clickable and/or focusable? Maybe it helps:

mFrameMute.setClickable(true);
mFrameMute.setFocusable(true);

If you don't manage to get it working an ugly but not-so-bad approach would be to change the background of the FrameLayout programmatically.

Upvotes: 1

Related Questions