Andrain
Andrain

Reputation: 910

Unable to change button background color on click

I know this topics has many answers I have tried every possible solution but unable to solve the problem. I have six buttons and on clicking the button I want to change the background color, border and text color of the button. When I click the button the color changes and disappears I want it to stay until user press any other button. Kindly review my code.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatButton
             android:id="@+id/btn1"
             android:layout_width="50dp"
             android:layout_height="30dp"
             android:gravity="center"
             android:text="Button 1"
             android:background="@drawable/btn_selector"
             android:textColor="@color/text_color_selector"
             android:textSize="12sp" />
</LinearLayout>

btn_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_selected" android:state_pressed="true" />
    <item android:drawable="@drawable/btn_selected" android:state_focused="true" />
    <item android:drawable="@drawable/btn_unselected" android:state_pressed="false" />
    <item android:drawable="@drawable/btn_unselected" android:state_selected="false" />
</selector>

btn_selected.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
     <solid android:color="@android:color/white" />
     <stroke android:width="0.5dp" android:color="#4484F4" />
     <corners android:radius="100dip" />
</shape>

btn_unselected.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
     <solid android:color="@color/colaba_grey_fill" />
     <stroke android:width="0.5dp" android:color="#4484F4" />
     <corners android:radius="100dip" />
</shape>

Upvotes: 1

Views: 483

Answers (2)

Zain
Zain

Reputation: 40898

You can do this programmatically by setting the drawable using ContextCompat.getDrawable() method:


public class MainActivity extends AppCompatActivity {

    private Drawable mDefaultButtonColor;
    private Drawable mSelectedButtonColor;
    private Button buttons[];

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

        // Adding all buttons in an array
        buttons = new Button[]{
                findViewById(R.id.btn1),
                findViewById(R.id.btn2),
                findViewById(R.id.btn3),
                findViewById(R.id.btn4)
        };
    }

    // Method to set the drawable
    private void toggleButton(Button button, boolean isSelected) {
        button.setBackground(isSelected ?
                ContextCompat.getDrawable(this, R.drawable.btn_selected)
                : ContextCompat.getDrawable(this, R.drawable.btn_unselected));
    }

    // Callback for all the buttons using `android:onClick` XML attribute
    public void onButtonClick(View view) {
        for (Button button : buttons)
            toggleButton(button, false);
        toggleButton((Button) view, true);
    }

}

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007554

When I click the button the color changes and disappears I want it to stay until user press any other button

You have no code to do that. A Button does not have a durable state that changes when the use clicks on it. You will have to do something yourself, in Java/Kotlin code, to do that.

For example, you could toggle the activated state via setActivated() on the Button. Then, in your btn_selector resource, you could have different drawables for android:state_activated="true" than for android:state_activated="false".

Upvotes: 2

Related Questions