Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Button highlight?

Tags:

android

I set a image as Button backgroud,but Button has no highlight when click it. Is there any way to solve it.

like image 987
Nick Avatar asked Nov 09 '10 08:11

Nick


People also ask

How do I highlight text in a button?

To highlight text using your mouse, position your cursor at the beginning of the text you want to highlight. Press and hold your primary mouse button (commonly the left button). While holding the mouse button, drag the cursor to the end of the text and let go of the mouse button.

How do I add color to a button?

All style elements in your HTML button tag should be placed within quotation marks. Type background-color: in the quotation marks after "style=". This element is used to change the background color of the button. Type a color name or hexadecimal code after "background-color:".

How do I highlight a clicked link button in CSS?

The :active selector is used to select and style the active link. A link becomes active when you click on it. Tip: The :active selector can be used on all elements, not only links.


2 Answers

If this is an ImageButton and you don't want to use several drawable for each pressed/unpressed state you could use the color filter attibute of images. This solution is similar to the one used by Omar.

Create an OnTouchListener modifying the color filter on touch.

public class ButtonHighlighterOnTouchListener implements OnTouchListener {

  final ImageButton imageButton;

  public ButtonHighlighterOnTouchListener(final ImageButton imageButton) {
    super();
    this.imageButton = imageButton;
  }

  public boolean onTouch(final View view, final MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
      //grey color filter, you can change the color as you like
      imageButton.setColorFilter(Color.argb(155, 185, 185, 185));
    } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
      imageButton.setColorFilter(Color.argb(0, 185, 185, 185)); 
    }
    return false;
  }

}

Assign this listener to your button:

  myButton = (ImageButton) findViewById(R.id.myButton);
  myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton));

Update

Improved class to apply highlighter to ImageView, ImageButton or TextView via its compound Drawable.

public class ButtonHighlighterOnTouchListener implements OnTouchListener {

  private static final int TRANSPARENT_GREY = Color.argb(0, 185, 185, 185);
  private static final int FILTERED_GREY = Color.argb(155, 185, 185, 185);

  ImageView imageView;
  TextView textView;

  public ButtonHighlighterOnTouchListener(final ImageView imageView) {
    super();
    this.imageView = imageView;
  }

  public ButtonHighlighterOnTouchListener(final TextView textView) {
    super();
    this.textView = textView;
  }

  public boolean onTouch(final View view, final MotionEvent motionEvent) {
    if (imageView != null) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        imageView.setColorFilter(FILTERED_GREY);
      } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        imageView.setColorFilter(TRANSPARENT_GREY); // or null
      }
    } else {
      for (final Drawable compoundDrawable : textView.getCompoundDrawables()) {
        if (compoundDrawable != null) {
          if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            // we use PorterDuff.Mode. SRC_ATOP as our filter color is already transparent
            // we should have use PorterDuff.Mode.LIGHTEN with a non transparent color
            compoundDrawable.setColorFilter(FILTERED_GREY, PorterDuff.Mode.SRC_ATOP);
          } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            compoundDrawable.setColorFilter(TRANSPARENT_GREY, PorterDuff.Mode.SRC_ATOP); // or null
          }
        }
      }
    }
    return false;
  }

}
like image 106
L. G. Avatar answered Sep 22 '22 21:09

L. G.


See here

And also here for Romain Guy's answer:

In res/drawable, create a file called for instance mybutton_background.xml and put something like this inside:

<?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="false" 
        android:drawable="@drawable/button_background_focus" /> 

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

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

    <item android:drawable="@drawable/button_background_normal" /> 

</selector> 

Then set this drawable as the background of your button with android:background="@drawable/mybutton_background"

like image 41
Lior Iluz Avatar answered Sep 21 '22 21:09

Lior Iluz