• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Cannot set Button background color in Android Studio

I got the onClick to work. I didn't have the word android: to the left of onClick - silly me.

But I still can't change the background color.
 
Using onClick in xml has been deprecated. You should be creating your buttons in Java. Something like this in your onCreate.
Java:
Button btn = findViewById(R.id.myBtn);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // do some stuff
    }
});

Add the id to your button xml layout instead of having onClick.
XML:
android:id="@+id/myBtn"

As for the background color, I don't remember too well since I replaced buttons with the CardView which allows you to do fancier stuff.

In CardView you choose your background like so.
XML:
card_view:cardBackgroundColor="?attr/colorPrimary"
or...
XML:
card_view:cardBackgroundColor="#FFFFFF"

So with Buttons it should be something similar.
XML:
android:BackgroundColor="?attr/colorPrimary"
or...
XML:
android:BackgroundColor="#FFFFFF"

Cheers.
 
Back
Top Bottom