how to make an imageview clickable in an listview
Hi i have created a basic listview and added a textview a开发者_如何学Gond imageview in it.
<ImageView
android:id="@+id/icon"
android:layout_width="50px"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:layout_height="wrap_content"
android:layout_alignParentRight = "true"
android:src="@drawable/call"
/>
I need to make imageview clickable so that if some clicks on it an action will happen like an new activity is opened.Can someone help me how to do this.
Thanks
You have to implement your own cursor adapter, and in that you have to override the getView
method and then set the onclick
listener to your image:
public class SMSimpleCursorAdapter extends SimpleCursorAdapter{
Context context;
Activity activity;
public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
long id=getItemId(position);
ImageView image= (ImageView)view.findViewById(R.id.icon);
image.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
}
}
Use ImageButton
instead:
<ImageButton
android:id="@+id/icon"
android:layout_width="50px"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:layout_height="wrap_content"
android:layout_alignParentRight = "true"
android:background="@drawable/call" />
Rishi,
Try something like the following:
ImageView myImg= (ImageView) findViewById(R.id.icon);
myImg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Launch Intent or whatever you want here
}
});
To make an ImageView
clickable you can set the property android:clickable="true"
in your ImageView
.xml
try this for your imageview : it worked for me
android:focusable = "false"
this works:
ImageView myImg= (ImageView) findViewById(R.id.icon);
myImg.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(YourCurrentActivity.this,YourNextActivity.class);
startActivity(i);
}
});
精彩评论