Change Button Background

Asked

Viewed 1,896 times

2

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

5 answers

1

You need to create a Selector and put it as 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>
  • Thanks for your help, but I’ve just seen a few more things I’ll have to do here. So how could I make this change using Swit?

  • 1

    What would Swit be?

1

First create the shapes: Shape of Button (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>

And 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 lastly, go to the code (.xml) of the button that will receive these backgrounds and add the following:

android:background="@drawable/seleccionador"

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);
            }

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 drawing of the button.

0

Programmatically you can use a flag. So 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

enter image description here enter image description here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.