Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between setTextAppearance from code vs. resource

Tags:

android

When I call setTextAppearance(this, android.R.style.TextAppearance_Medium) programmatically I get medium sized font with light grey text.

However, when I use android:textAppearance="@android:style/TextAppearance.Medium" in the xml then I get the same sized text but it is colored black.

What is the difference between these?

The relevant part of the xml file looks as following:

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@android:style/TextAppearance.Medium"
    android:text="Button" />

The code looks like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button)findViewById(R.id.button1);
    button.setText("This is a long text");
    //button.setTextAppearance(this, android.R.style.TextAppearance_Medium);
}
like image 212
user204884 Avatar asked Dec 18 '11 21:12

user204884


2 Answers

the correct response is setTextAppearance(context, android.R.style.TextAppearance_Large);

like image 180
user1154390 Avatar answered Nov 10 '22 00:11

user1154390


It should not be different. The only way to get a different color is to either:

1) change the text color programatically after your setTextAppearance

or

2) Your xml files contains a android:textColor with black.

If your xml code contains the android:textColor attribute, Android will use that one, rather than the one defined in xml. If you manually setTextAppearance, the color of the style will be used.

Hence, you probably have android:textColor="#000000" in the xml for that element

like image 22
Entreco Avatar answered Nov 09 '22 23:11

Entreco