Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: setBackgroundColor without removing my drawable

I have a button:

<Button
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:text="@string/male"
    android:id="@+id/btn_mgen_m"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/bg_tgl_btn_left"
    android:textColor="#444444" />

and I changed its color like this:

btnMgenM = (Button) findViewById(R.id.btn_mgen_m);
btnMgenM.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        btnMgenM.setBackgroundColor(Color.parseColor("#dca8c3"));
    }
});

The background color is changed properly but it seems to remove the background drawable. The round radius, text color and border are removed just the simple colored button is left. I want to change the background color when I click this button, but not change anything else.

How can I do this?

like image 808
abend0 Avatar asked Nov 09 '22 14:11

abend0


1 Answers

Try this both of line

btnMgenM.setBackgroundResource(0);
btnMgenM.setBackgroundColor(Color.parseColor("#dca8c3"));
like image 193
Masum Avatar answered Nov 15 '22 06:11

Masum