Reputation: 16310
I have a number of ImageButtons
in a layout. The image resources are 32x32 px. They all have the same attributes:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ibButton1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="0"
android:src="@drawable/not_selected"
android:layout_gravity="center_vertical"
android:background="@null"/>
In my fragment, I'm doing:
final ImageButton button1 = (ImageButton)view.findViewById(R.id.ibButton1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isSelected = !isSelected;
if (isSelected ) {
button1.setImageResource(R.drawable.selected);
}
else {
button1.setImageResource(R.drawable.not_selected);
}
}
});
However, most times I need to click over 5 times on the button for it to register the click.
Should I need to increase the image size or is there a better way to listen to clicks? Should I use onClick
attribute instead?
Upvotes: 0
Views: 116
Reputation: 1431
Extract Image With Touch zone I Mean Transparent Background for Eg : the Give Image is 86 * 70 px So When Ever User touches round that Transparent background 86* 70px you can fire event
Upvotes: 0
Reputation: 5294
It seems to me , you don`t need an ImageButton, but a ToggleButton with to states. http://developer.android.com/guide/topics/ui/controls/togglebutton.html
and please remove android:clickable="true" , it is not needed, because it is a button, which mean, it is true by default. If it would be an TextView, it would needed.
Upvotes: 0
Reputation: 154
Perhaps, the size of the image is too small(as @Shoeb Siddique said) to get touch event on the image. You can increase the padding or size of image to improve the probability to touch the image.
Upvotes: 0
Reputation: 2825
Provide some padding to increase the Tap area.
android:padding="10dp"
Upvotes: 3