Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change statelistdrawable text color android button

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 ?

like image 409
Amira Elsayed Ismail Avatar asked Jan 30 '13 12:01

Amira Elsayed Ismail


People also ask

How to customize a button to set text and color in Android?

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

How to change the text color in button widget?

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.

What is%colorstatelist in Salesforce?

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.

How to change the orientation of a button in Android?

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.


1 Answers

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"

like image 58
ben75 Avatar answered Sep 22 '22 07:09

ben75