How to make the button backlight?

I set the image as backgroud Button, but Button does not highlight the selection when it is clicked. Is there any way to solve it.

+16
android
Nov 09 '10 at 8:22
source share
7 answers

To access the correct resource, you will find the StateList Drawable documentation. You simply create and define it in your res/drawable and set it as the background of the buttons.

+10
Nov 09 '10 at 8:59
source share

If it is ImageButton, and you do not want to use several available for each pressed / not pressed state, you can use the filter of color images. This solution is similar to that used by Omar.

Create an OnTouchListener by changing 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 a button to this listener:

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

Update

Improved class for applying marker to ImageView, ImageButton or TextView through its composite 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; } } 
+20
Jan 11 '13 at
source share

Look here

And also here for Romain Guy answer:

In res / drawable, create a file called mybutton_background.xml for example 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 value as the background of your button using android:background="@drawable/mybutton_background"

+13
Nov 09 '10 at 8:23
source share

if you use

 <Button ------------------ android:background="@drawable/youimage" > 

Then, what is the best way to declare a new new xml. With an image, you can specify an image for each event state . as Octavian says and you can set this xml as background

if your xml is 'res / drawable / abc.xml' then set the background as

 android:background="@drawable/abc" 

You can also use ImageButton

 <ImageButton ---------------------- android:src="@drawable/youimage" /> 

The advantage of ImageButton is that there is no need for another image to highlight.

+4
Nov 09 '10 at 9:55
source share

Try this to get a bad person effect without requiring multiple copies of your images. Set the alpha value of 0.8 to the desired value:

 import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ImageButton; public class EAImageButton extends ImageButton { public EAImageButton(Context context) { super(context); // TODO Auto-generated constructor stub } public EAImageButton(Context context, AttributeSet attrs){ super(context,attrs); } public EAImageButton(Context context, AttributeSet attrs, int defStyle){ super(context,attrs,defStyle); } public void setImageResource (int resId){ super.setImageResource(resId); } @Override public boolean onTouchEvent(MotionEvent e){ if(e.getAction() == MotionEvent.ACTION_DOWN){ this.setAlpha((int)( 0.8 * 255)); }else if(e.getAction() == MotionEvent.ACTION_UP){ this.setAlpha((int)( 1.0 * 255)); } return super.onTouchEvent(e); } } 
+3
Oct 22
source share

I did this by overriding the Button class

 public class MButton extends Button { public MButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MButton(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MButton(Context context) { super(context); // TODO Auto-generated constructor stub } //define style public void setPressedBg(Integer[] mImageIds) { StateListDrawable bg = new StateListDrawable(); Drawable normal = this.getResources().getDrawable(mImageIds[0]); Drawable selected = this.getResources().getDrawable(mImageIds[1]); Drawable pressed = this.getResources().getDrawable(mImageIds[2]); bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed); bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected); bg.addState(View.ENABLED_STATE_SET, normal); bg.addState(View.FOCUSED_STATE_SET, selected); bg.addState(View.EMPTY_STATE_SET, normal); this.setBackgroundDrawable(bg); } //define style public void setPressedBg(Integer p1,Integer p2,Integer p3) { StateListDrawable bg = new StateListDrawable(); Drawable normal = this.getResources().getDrawable(p1); Drawable selected = this.getResources().getDrawable(p2); Drawable pressed = this.getResources().getDrawable(p3); bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed); bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected); bg.addState(View.ENABLED_STATE_SET, normal); bg.addState(View.FOCUSED_STATE_SET, selected); bg.addState(View.EMPTY_STATE_SET, normal); this.setBackgroundDrawable(bg); } 

}

in my main ()

 toneButton.setPressedBg(R.drawable.button_tone_protrait_pipa, R.drawable.button_tone_protrait_pipa1, R.drawable.button_tone_protrait_pipa1); 
+2
Nov 15 '10 at 7:21
source share

Use View.setFocusableInTouchMode or android: focusableInTouchMode . In this case, use this part for your button, either in XML format or in the code itself. For more information see http://developer.android.com/reference/android/view/View.html

0
Nov 09 '10 at 11:54 on
source share



All Articles