• 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

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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones