Android - same OnClickListener for TextView and ImageView
android, onclicklistener
Solution
you can add one parent view for both view and call click listener to that parent view like you can add LinearLayout as parent:
<LinearLayout android:id="@+id/firstparent">
<Imageview/>
<TextView/>
</LinearLayout>
<LinearLayout android:id="@+id/secondparent">
<Imageview/>
<TextView/>
</LinearLayout>
<LinearLayout android:id="@+id/thirdparent">
<Imageview/>
<TextView/>
</LinearLayout>
<LinearLayout android:id="@+id/forthparent">
<Imageview/>
<TextView/>
</LinearLayout>
Now set the click listener to `LinearLayout` so you can get the same event whether you click the imageview or textview.
then register the onClickListner for each layout like.
layout1.setOnClickListener(this);
layout2.setOnClickListener(this);
layout3.setOnClickListener(this);
layout4.setOnClickListener(this);
@Override
public void onClick ( View v )
{
if ( v == layout1 )
{
// your code...
}
else if(v == layout2){
// your code...
}
//// add others...
}
Problem
I have a list of 3 items: ``` [image_1] [text_1] [image_2] [text_2] [image_3] [text_3] ``` I'm not using a ListView. Instead I used a RelativeLayout. How would I handle an OnClickListener for both the TextView and ImageView? list_layout.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/image_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="21dp" android:layout_marginTop="21dp" android:src="@drawable/calbutton" /> <TextView android:id="@+id/text_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/image_1" android:layout_toRightOf="@+id/image_1" android:text="TextView" /> <ImageView android:id="@+id/image_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBelow="@+id/image_1" android:layout_below="@+id/image_1" android:layout_marginLeft="21dp" android:layout_marginTop="21dp" android:src="@drawable/calbutton" /> <TextView android:id="@+id/text_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/text_1" android:layout_alignTop="@+id/image_2" android:text="TextView" /> <ImageView android:id="@+id/image_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBelow="@+id/image_2" android:layout_below="@+id/image_2" android:layout_marginLeft="21dp" android:layout_marginTop="21dp" android:src="@drawable/calbutton" /> <TextView android:id="@+id/text_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/image_3" android:layout_toRightOf="@+id/image_3" android:text="TextView" /> </RelativeLayout> ```