user2668638
user2668638

Reputation: 653

How to change button background in java?

I have xml file in drawable folder for rounded button - rounded.xml.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ccc" />
    <corners android:radius="5dp" />
    <stroke android:width="1dp" android:color="#000" />
</shape>

Usually I set it by using android:background = "@drawable/rounded" inside button xml, but I would like to create few xml files with different colors so that I could change app theme.

Do you know how should I set background for button, but in java?

Upvotes: 1

Views: 3391

Answers (1)

Or B
Or B

Reputation: 1803

This should work:

Button button = (Button) findViewById(R.id.myButton);
button.setBackgroundResource(R.drawable.rounded);

Upvotes: 6

Related Questions