ImageButton not always clickable on the first click

116 views Asked by At

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?

4

There are 4 answers

0
Shoeb Siddique On BEST ANSWER

Provide some padding to increase the Tap area.

android:padding="10dp"
0
Arun On

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.

2
narancs On

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.

0
Anil Meenugu On

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

enter image description here