Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let click event pass to container in android?

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);
            }
        }
like image 550
frmi Avatar asked Nov 28 '13 06:11

frmi


1 Answers

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.

like image 133
Techfist Avatar answered Sep 22 '22 12:09

Techfist