Change button background

2

How can I change the background of a Button by clicking on it? For example: I have shape01 and shape02 , for default the Button is with shape01 , when clicking it I need to change to shape02 , and clicking again return to shape01 .

    
asked by anonymous 19.09.2016 / 14:36

5 answers

1

First create the shapes: Button Shape (normal):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

        <gradient
            android:angle="270"
            android:startColor="#2e8fc2"
            android:endColor="#006599"
            />

        <stroke
            android:width="2dp"
            android:color="#00679e"
            />

        <corners
            android:radius="3dp"
            />

</shape>

And after the button when pressed:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="270"
        android:startColor="#254683"
        android:endColor="#1f2f61"
        />

    <stroke
        android:color="#000000"
        android:width="2dp"
        />

    <corners
        android:radius="3dp"
        />

</shape>

Then, create a selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

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

</selector>

And finally, go to the code (.xml) of the button that will receive these backgrounds and add the following:

android:background="@drawable/seleccionador"
    
19.09.2016 / 15:09
1

You need to create a Selector and put it as the background button.

Example:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:enterFadeDuration="@android:integer/config_shortAnimTime"
    android:exitFadeDuration="@android:integer/config_shortAnimTime">

    <item android:state_checked="true" android:color="@color/white" />
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:state_enabled="false"  android:color="@color/colorAccent" />
    <item android:color="@color/colorAccent" />

</selector>
    
19.09.2016 / 14:38
0

You can programmatically use flag . Then you can declare for example, colorFlag = 0 . So you can set the color using setBackgroundColor() . Every time you change click, you can change a different color. If you prefer too, you can put as many colors as you want at the time of the click.

Main.java

public class Main extends Activity {

    private Button btn;
    private int colorFlag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (colorFlag == 0) {
                    btn.setBackgroundColor(Color.parseColor("#00ff00")); // verde
                    colorFlag = 1;
                } else {
                    btn.setBackgroundColor(Color.parseColor("#ffff00")); // amarelo
                    colorFlag = 0;
                }
            }
        });
    }
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#ffff00"
        android:text="Button" />

</RelativeLayout>

Screenshots

    
19.09.2016 / 15:50
0

Try

int color = 0x8000FF00; // 50% verde
Button button = (Button) findViewById(R.id.btnTeste);

button.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);

The result is very good and you do not lose the original button design.

    
20.09.2016 / 14:33
0

To change the button icon of my application I used this code.

if(mp.isPlaying()==true) {
                bTocar.setBackgroundResource(R.drawable.play);

            }else{                   
                bTocar.setBackgroundResource(R.drawable.pause);
            }
    
10.03.2018 / 16:31