Reputation: 1127
I am working on app with ListView with buttons on list item.
It's working fine but I can't find way to make buttons on items clickable.
I want to make this Silver arrows clickable. How can I do that?
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:layout_centerVertical="true"/>
How can I make this to be different, like ItemClickListener with position?
Here is my Adapter
class DAdapter extends ArrayAdapter<String>{
Context context;
int [] images;
String[] titleArray;
DAdapter(Context c, String [] titles, int imgs[])
{
super(c,R.layout.single_row, R.id.title,titles);
this.context=c;
this.images=imgs;
this.titleArray=titles;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
if (row==null) { LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.single_row, parent,false);
}
ImageView myImage=(ImageView) row.findViewById(R.id.list_image);
TextView myTitle=(TextView) row.findViewById(R.id.title);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
return row;
I'm not sure if I should add whole code but if its needed please tell me.
single_row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text=""
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:layout_centerVertical="true"/>
</RelativeLayout>
Upvotes: 0
Views: 611
Reputation: 14590
First add id to your ImageView like..
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentRight="true"
android:id="@+id/arrowImage"
android:onClick="btnclick"
android:layout_centerVertical="true"/>
then get the ImageView and write listener ..change your getView like..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row, parent, false);
}
ImageView myImage = (ImageView) row.findViewById(R.id.list_image);
TextView myTitle = (TextView) row.findViewById(R.id.title);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
ImageView arrowImage = (ImageView) row.findViewById(R.id.arrowImage);
arrowImage.setTag(position);
arrowImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int clickedposition=(Integer) v.getTag();
//Write your Activity launch code here..
}
});
return row;
}
Upvotes: 1
Reputation:
You should set a general onclick handler for the image view. Then while doing the job in the onclick listener just check its parent to know the parent of this button. And if you want some custom style for this arrow to be shown, you can use a button instead of imageButton and set its background to an xml file to make it dynamic. Is this enough?
Upvotes: 0