Reputation: 6509
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>
Upvotes: 3
Views: 5633
Reputation: 12269
You can either set the onClick event like below for both the image/button/textview/edittext etc and have the method in your Activity.
<Button
android:id="@+id/some_ui_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:onClick="onClickButton"
android:shadowColor="#000"
android:text="@string/sometitle"
android:textColor="@android:color/primary_text_dark_nodisable" />
/**
* onClickView() called
* @param v
*/
public void onClickView(View v) {
//Do something
switch(v.getId()){
case R.id.someuielement :
break;
...
...
}
}
Upvotes: 0
Reputation: 10959
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...
}
Upvotes: 5
Reputation: 6409
For ListView
you can add an onItemClickListener
that will be called for a click anywhere on the row.
Upvotes: 2
Reputation: 995
This is the code
TextView tv[] = new TextView[subCategory.length];
for (int i = 0; i < subCategory.length; i++) {
tv[i] = new TextView(this);
tv[i].setText(subCategory[i]);
tv[i].setId(i);
sCategoryLayout.addView(tv[i]);
tv[i].setOnClickListener(onclicklistener);
}
onclicklistener method :
OnClickListener onclicklistener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == tv[0]){
//do whatever you want....
}
}
};
Upvotes: 0
Reputation: 1098
If you want one handler with different actions for each View, you could do this:
TextView tv;
ImageView iv;
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
public void onClick(View v) {
if( tv.getId() == ((TextView)v).getId() ){
// it was the textview
}
else if(iv.getId() == ((ImageView)v).getId() ){
// it was the imageview
}
}
}
Or if you want to group the two Views together, put them in a container (e.g. RelativeLayout, LinearLayout, etc) and assign an OnClickListener to the container.
Upvotes: 0
Reputation: 5803
Put the imageview
and textview
inside a linear layout
. then override onClickLListener
for the LinearLayout
<RelativeLayout ...... >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical" >
<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" />
</LinearLyaout>
.......
</RelativeLayout>
Upvotes: 2
Reputation: 22493
just use OnClickListeners for all textviews and imageviews like this
findViewById(R.id.image_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
////// write your code here
}
});
Upvotes: 1
Reputation: 443
Just implements the OnClickListener()
to your java file like beflow,
public yourClassName extends Activity implements OnClickListener
{
....your code
// register for onClickListener
text1.setOnClickListener(this);
// register same for other textboxes & imageview.
@Override
public void onClick ( View view )
{
if ( view == text1 ) // assuming text1 is your text_1 of your .xml file
{
// do stuff
}
...
// same for other textboxes & image view.
}
}
Upvotes: 1