Janwel
Janwel

Reputation: 300

Android drawable selector state pressed is true the button must change background color

This must be a silly question as there are so many related question here in stackoverflow and maybe the answer is simple. But anyway here it is. I've got a button and create a drawable xml for it. So under my button here is the code:

<Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="30dip"           
            android:background="@drawable/button2" 
            />

Now under my drawable/button2 i have created a selector in which i am not using an image. Here is my code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#FF9E35" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
 </item>
 <item>
    <shape>
        <gradient
            android:startColor="#FF9E35"
            android:endColor="#992f2f"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
    </item>
   </selector>

Now what I am trying to accomplish is that when I clicked that button, it change its background color to what is state_pressed is implying and return to normal if another button is clicked(Most likely a toggle button) Is there anyway to do it on code behind or in xml selector?

Thanks,

JC

Upvotes: 1

Views: 4101

Answers (1)

Alexis C.
Alexis C.

Reputation: 93842

What you can actually do in Java code to accomplish it is :

public class LaunchActivity extends Activity implements OnTouchListener{

    private Button yourButton;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);      

        yourButton= (Button)findViewById(R.id.yourButton);
        yourButton.setOnTouchListener(this); 

    }

    @Override
    public boolean onTouch(final View view, MotionEvent event) {

    final int action = event.getAction();

        if(view.getId()==R.id.yourButton){
            if(action == MotionEvent.ACTION_DOWN)
                  yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
            if(action == MotionEvent.ACTION_UP)
                  yourButton.setBackgroundResource(R.drawable.ic_button_normal);
        }

 }
}

That will change the background of the button if he's pressed. When the user release his finger of it, it will change the background again. If you want to change the background only if another button is pressed, don't write the ACTION_UP case and in the other ACTION_DOWN case of another button, change the background of other button, see this below :

 @Override
        public boolean onTouch(final View view, MotionEvent event) {

    final int action = event.getAction();

        if(view.getId()==R.id.yourButton){
            if(action == MotionEvent.ACTION_DOWN){
                  secondButton.setBackgroundResource(R.drawable.ic_button_normal);
                  yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
            }
        }
            if(view.getId()==R.id.secondButton){
            if(action == MotionEvent.ACTION_DOWN){
                  yourButton.setBackgroundResource(R.drawable.ic_button_normal);
                  secondButton.setBackgroundResource(R.drawable.ic_button_pressed);
            }
        }

 }

Hope it helps !

Upvotes: 2

Related Questions