How to let click event pass to container in android?
Asked Answered
F

3

7

I have a table of FrameLayout where each frame contains either an ImageView or a TextView. Regardless of content in the frame I want the onClick event to be detected by the OnClickListener set on the frame.

How can I achieve this?

This is some of my layout, I got a total of 5 rows (Here's only shown 1). As described above I have 5 FrameLayouts in each row, each containing a TextView. I am not able to put my OnClickListener on the TextView since this may be changed to an ImageView at runtime. Therefore i want the OnClickListener on the FrameLayout.

It seems that the content of the FrameLayout is preventing it from detecting the click event.

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/gateContainer"
        android:stretchColumns="*">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/door1"
                android:clickable="true">

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="New Text"
                    android:id="@+id/textView"
                    android:layout_gravity="center" />

            </FrameLayout>

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/door2" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="New Text"
                    android:id="@+id/textView5"
                    android:layout_gravity="center" />
            </FrameLayout>

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/door3" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="New Text"
                    android:id="@+id/textView4"
                    android:layout_gravity="center"/>
            </FrameLayout>
     </TableLayout>

Here is an example of how i set the OnClickListeners:

        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v){
                // do something
            }
        };

        TableLayout tableLayout = (TableLayout) findViewById(R.id.gateContainer);
        // Go through all TableRows
        for (int i = 0; i < tableLayout.getChildCount(); i++){
            TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
            // Set listener for each FrameView
            for (int j = 0; j < tableRow.getChildCount(); j++){
                FrameLayout frame = (FrameLayout) tableRow.getChildAt(j);

                frame.setOnClickListener(clickListener);
            }
        }
Forum answered 28/11, 2013 at 6:50 Comment(2)
Post some code for us to see what you have tried so far.Philtre
@GrIsHu code has been added.Forum
H
8

You could also set android:clickable="false" to the TextView/ImageView layouts and android:clickable="true" on the background, that way the background view always catches the clicks.

Additionally, the following answer might be helpful for people considering this issue:

SO: Android: How to propagate click event to LinearLayout childs and change their drawable

Hartmunn answered 27/3, 2014 at 10:33 Comment(1)
This should be the accepted answer, since messing around with TouchEvents is a little bit hacky!Judon
A
8

Just implement, OnTouchListener over ImageView and TextView components and duck them, in the sense pass them.

<your_view>.setOnTouchListener(new OnTouchListener(){

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return true;
    }

}); 

this will make sure, your container handles that.

Ahmadahmar answered 28/11, 2013 at 6:55 Comment(1)
Thank you, this was what i was looking for. I changed my OnClickListener to an OnTouchListener.Forum
H
8

You could also set android:clickable="false" to the TextView/ImageView layouts and android:clickable="true" on the background, that way the background view always catches the clicks.

Additionally, the following answer might be helpful for people considering this issue:

SO: Android: How to propagate click event to LinearLayout childs and change their drawable

Hartmunn answered 27/3, 2014 at 10:33 Comment(1)
This should be the accepted answer, since messing around with TouchEvents is a little bit hacky!Judon
W
0

You can define the click event on your TextView or ImageView as below:

   <yourView>.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // your logic here.

        }
    });
Wille answered 28/11, 2013 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.