I'm developing android application
I have a different background drawable and text color for each status of the button (pressed , normal)
I have created statelistdrawable object to be able to add the background drawable , but my problem now is how to set the text color
can any one help please ?
This example demonstrates how do I customize a button to set text and color in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java
To change the text color in Button widget, set the textColor attribute with required Color value. You can specify color value in rgb, argb, rrggbb, or aarrggbb format.
ColorStateList is an object which can define in an XML file that can be used to apply different colors on widgets (such as Buttons, etc) depending on the state of Widgets to which it is being applied.
Go to the layout folder and in the activity_main.xml file change the ConstraintLayout to LinearLayout and give its orientation vertical. Add the Button and Switch to the layout.
Button is a TextView and you can call button.setTextColor(someColorStateList)
on a TextView.
Here is how to do it programmatically:
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{R.attr.state_pressed},
new int[]{R.attr.state_selected},
new int[]{-R.attr.state_selected},
},
new int[]{
Color.GREEN,
Color.BLUE,
Color.RED});
TextView textView = ... some textView or button
textView.setTextColor(colorStateList);
You can of course do the same with xml configuration (define your colorStateList in xml and associate it with the button text color property android:textColor="@drawable/mycolorstatelist"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With