Programmatically change button background drawable onClick
private boolean isButtonClicked = false; // You should add a boolean flag to record the button on/off state
protected void onCreate(Bundle savedInstanceState) {
......
Button star = (Button) findViewById(R.id.buttonStar);
star.setOnClickListener(new OnClickListener() { // Then you should add add click listener for your button.
@Override
public void onClick(View v) {
if (v.getId() == R.id.buttonStar) {
isButtonClicked = !isButtonClicked; // toggle the boolean flag
v.setBackgroundResource(isButtonClicked ? R.drawable.btn_star_on : R.drawable.btn_star_off);
}
}
});
}
android set button background programmatically
R.color.red
is an ID (which is also an int
), but is not a color.
Use one of the following instead:
// If you're in an activity:
Button11.setBackgroundColor(getResources().getColor(R.color.red));
// OR, if you're not:
Button11.setBackgroundColor(Button11.getContext().getResources().getColor(R.color.red));
Or, alternatively:
Button11.setBackgroundColor(Color.RED); // From android.graphics.Color
Or, for more pro skills:
Button11.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB
Android: change button background programmatically
You could use an xml file like one below, to create states for your button.
The info about attributes available is here.
You simply copy this xml file to your drawables folder in your project, name it for example custom_button.xml, and reference it in your layout with
android:background="@drawable/custom_button"
Here is xml file...
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<solid
android:color="#00ff00" />
<stroke
android:width="5dp"
android:color="#ff0000"
android:dashWidth="3dp"
android:dashGap="2dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#ffffff"
android:centerColor="#ffffff"
android:startColor="#ffffff"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#00ff00" />
<corners
android:radius="5dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#ffffff"
android:centerColor="#ffffff"
android:startColor="#ffffff"
android:angle="270" />
<stroke
android:width="5dp"
android:color="#00ff00" />
<corners
android:radius="5dp" />
</shape>
</item>
Change oval shaped button background programmatically
You could modify it using this simple method down below
GradientDrawable BackgroundShape = (GradientDrawable)btn.getBackground();
BackgroundShape.setColor(Color.BLACK);
Setting Custom-Theme to Button-Background programmatically
In case someone faces the same problem, I found another question which gave me the hint: https://stackoverflow.com/a/53884954/7671760
Using typeArry buttonDraw
I saved the default button theme in onCreate
-->
buttonDraw = getTheme().obtainStyledAttributes(new int[]{R.attr.button});
then changed my resetColor()
to:
buttonA2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonB2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonC2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonD2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
Don't know if that's the smartest way, but it worked for me.
Cheers!
How to add button tint programmatically
According to the documentation the related method to android:backgroundTint
is setBackgroundTintList(ColorStateList list)
Update
Follow this link to know how create a Color State List Resource.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#your_color_here" />
</selector>
then load it using
setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));
where contextInstance
is an instance of a Context
using AppCompart
btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));
Related Topics
Android Beginner Difference Between Padding and Margin
Com.Google.Android.Gsf Package Couldn't Be Found
Signal 11 Sigsegv Crash Android
How to Get Current Buildtype in Android Gradle Configuration
Android: Simulate Wifi in the Emulator
Convert Time Value to Format "Hh:Mm Am/Pm" Using Android
Text/Design Tab Missing New Android Project on Android Studio Ide
Google Play App Description Formatting
Start Activity Using Custom Action
How to Finish an Android Application
How to Show Progress Dialog in Android
Find Out the Running Process Id by Package Name
How to Find Intent Source in Android
How to Add a Gesture Detector to a View in Android