Android highlight an imagebutton when clicked-ThrowExceptions

Exception or error:

I am using an ImageButton. But I don get the highlight when clicked. I googled and many suggested to use selector where another image is displayed. Is there any way around this. by using only one image and highlighting it or giving it a glow effect. so that the user knows that button has been clicked.

How to solve:

This is actually not very difficult to do. You don’t even need to create 2 seperate .png files or anything like that. For instance, if you want to have a button which has a gradient, and then change it when the button is pressed:

Step 1:
Create default button gradient (drawable/default_button.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>

Step 2: Create default button pressed gradient (drawable/default_button_pressed.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>

Step 3: Create selector (drawable/default_button_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/default_button_pressed" /> 
    <item android:drawable="@drawable/default_button" /> 
</selector>

Step 4 (optional): Create style for the button (values/style.xml):

<resources>
    <style name="DefaultButton">
        <item name="android:layout_width">wrap_content</item>   
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/default_button_selector</item>
    </style>
</resources>

Step 5: use the button (layout/main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" android:layout_height="fill_parent">

    <button style="@style/DefaultButton" />

</RelativeLayout>

As you can see, it’s not particularly difficult to do.

###

Without having to create multiple images (pressed, normal etc) and even don’t have to create selector. Use setOnTouchListener rather than setOnClickListener. The below code will give you the grey overlay on the clicked item.

 ((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() {

      @Override
        public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              ImageButton view = (ImageButton ) v;
              view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
              v.invalidate();
              break;
          }
          case MotionEvent.ACTION_UP:

              // Your action here on button click

          case MotionEvent.ACTION_CANCEL: {
              ImageButton view = (ImageButton) v;
              view.getBackground().clearColorFilter();
              view.invalidate();
              break;
          }
          }
          return true;
        }
    });

###

In order not to have to set several drawable for each button I set the color filter attribute of the image button in an on touch listener.

See code here in another post:

https://stackoverflow.com/a/14278790/891479

###

I managed to do this!!
First you declare buttonStyle.xml in drawable folder.

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:state_focused="true"
      android:state_pressed="true"
      android:drawable="@drawable/button_pressed" />

  <item
      android:state_focused="false"
      android:state_pressed="true"
      android:drawable="@drawable/button_pressed" />

  <item android:drawable="@drawable/button_normal" />
</selector>

Then you create button_pressed.xml in drawable folder.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
  <solid android:color="@color/semiTransparentGnfrBlueColor" />
  <stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>

After that, you create button_normal.xml in drawable folder.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:innerRadiusRatio="3"
  android:shape="rectangle">
  <solid android:color="@color/gnfrBlueColor"/>
  <stroke android:width="10dp" android:color="@android:color/transparent" />
</shape>

You can use default colors but if you want to do it like me, you need to have a file named colors.xml in values folder and this values inside:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="gnfrBlueColor" type="color">#2A3748</item>
  <item name="semiTransparentGnfrBlueColor" type="color">#602A3748</item>
<integer-array name="androidcolors">
<item>@color/gnfrBlueColor</item>
    <item>@color/semiTransparentGnfrBlueColor</item>
  </integer-array>
</resources>

Finally you set this to your button or whatever:

savedAccountLoginButton.SetBackgroundResource(Resource.Drawable.buttonStyle);

NOTICE THAT I set a stroke with color transparent because when you set backgroundresource your button becomes bigger and with this trick it remains original.

This is the only way I could archieve this programmatically.

If you want to do this by XML:

<Button
            android:text="ENTRAR"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/buttonstyle"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:id="@+id/button1" />

###

You can simply use android:foreground for your View to achieve clickable effect:

android:foreground="?android:attr/selectableItemBackground"

For use with dark theme add also theme to your layout (to make clickable effect clear):

android:theme="@android:style/ThemeOverlay.Material.Dark"

Leave a Reply

Your email address will not be published. Required fields are marked *