简体   繁体   中英

How do I change the text color of a Button?

How do I change the text color of a Button?

try this:

button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR

or

button.setTextColor(0xff0000); //SET CUSTOM COLOR 

or

button.setTextColor(Color.parseColor("#ff0000")); 

and in xml:

<Button android:id="@+id/mybtn" 
        android:text="text textx "  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"  
        android:textStyle="bold" 
        android:textColor="#ff0000" />  <-- SET TEXT COLOR HERE -->

Use the android:textColor property.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textColor="@android:color/white" />
button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));

this work too

Use: android:textColor="#FFFFFF" on the xml configuration,

or on the activity itself by calling

button.setTextColor(0xFFFFFF);

(FFFFFF is the color white).

For more color codes: here

You can use the android textColor for foreground and for background color of button, text view or any other element see code example

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="#ffb6c1"
        android:textColor="#fff"
        />

any hexadecimal color code can be written for making interactive view.

change button text color programmatically

button.setTextColor(getResources().getColor(R.color.colorWhite));

An easy way to do this is by defining the color you want in res/values/colors.xml in this way:

<color name="colorCyan">#00BCD4</color>

and the button should look this way:

<Button
    android:id="@+id/m_button"
    android:text="MY BUTTON"
    android:textColor="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorCyan"/>

Here's an approach with slightly less code that uses the implied Context of the current Activity.

button.setTextColor(getColor(R.color.colorPrimary));

I have not tested this with all API targets, but it is working for 28.

You can use:

button.setTextColor("green");

or

button.setTextColor(colorcode);

The format is AARRGGBB, so FF0000 is a transparent red. Use FFFF0000 instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM