0

I already know that if I have 2 images, I can switch the image when the button is selected or not (by using selector).

But with only 1 image.. I want to know whether I can change the color of the image when the button is selected. I have 4 buttons and I just want to indicate which button is selected. That is, I want the image to turn into gray when not selected, and back to its original color when selected. Is it possible with only 1 image??

심희수
  • 123
  • 1
  • 2
  • 5

3 Answers3

0

it is possible.

try this

Button mButton1, mButton21, mButton3, mButton4;
int mCurrentId = 0;

mButton1.setOnClickListener(mOnClickListener);
mButton2.setOnClickListener(mOnClickListener);
mButton3.setOnClickListener(mOnClickListener);
mButton4.setOnClickListener(mOnClickListener);

View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              mCurrentId = v.getId();
            switch (v.getId()) {
                case R.id.button1:
                     if(mCurrentId == v.getId()){
                     mButton1.setBackgound(Selectimage);
                     }else{
                     mButton1.setBackgound(DefaultImage);
                     }
                case R.id.button2:
                     if(mCurrentId == v.getId()){
                     mButton2.setBackgound(Selectimage);
                     }else{
                     mButton2.setBackgound(DefaultImage);
                     }
                case R.id.button3:
                     if(mCurrentId == v.getId()){
                     mButton3.setBackgound(Selectimage);
                     }else{
                     mButton3.setBackgound(DefaultImage);
                     }
                case R.id.button4:
                     if(mCurrentId == v.getId()){
                     mButton4.setBackgound(Selectimage);
                     }else{
                     mButton4.setBackgound(DefaultImage);
                     }
                     break;
             }

        }
    };
배준모
  • 591
  • 5
  • 12
0

make a XML layout in your drawable named: txt.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#424242"/>
</selector>

In button:

     <Button
        .....
        android:background="@drawable/txt"
        ...../>
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

You can do this my extending Button class with your code in onTouchEvent. In my example i am changing background and text color you can change image color by using Tint(color filter) on background image.

    public class MyButton extends Button {

    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MyButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void setEnabled(boolean enabled) {
    //        super.setEnabled(enabled);
        if (!enabled)
            setBackgroundColor(Color.GRAY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //when Button is Pressed
        if (!isPressed()) {
            setBackgroundColor(YOUR PRESSED COLOR);
            setTextColor(Color.BLACK);
        }else {//when Button Released
            setBackgroundColor(<YOUR INACTIVE COLOR>);
            setTextColor(Color.BLACK);
        }
        return super.onTouchEvent(event);
    }
}