73

I am going to implement a LinearLayout in which the input fields are programmatically generated according to the number of fields of the database table.

Unfortunately, when I am trying to set the attribute: textApperance as textApperanceLarge in the TextView, it doesn't work. Below is my code...

for (int i = 0; i < selectedProducts; i++) {

            premLayout[i] = new LinearLayout(this);
            premLayout[i].setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            premLayout[i].setOrientation(LinearLayout.HORIZONTAL);
            premLayout[i].setGravity(Gravity.TOP);

            premTextView[i] = new TextView(this);
            premTextView[i].setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    2.0f));
            premTextView[i].setTextAppearance(this, android.R.attr.textAppearanceLarge);

            premTextView[i].setText(premiumChannels.get(i));
            premTextView[i].setId(i + 600);

            int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
            premTextView[i].setWidth(px);

            premLayout[i].addView(premTextView[i]);
4
  • By not working, I assume you mean the text is just "normal sized"?
    – HalR
    Apr 29, 2013 at 3:36
  • the default size is TextAppearanceSmall...but I want to to set...TextAppearanceLarge.
    – Larry Lo
    Apr 29, 2013 at 3:36
  • you are building your own text appearance attribute or you are using from android. in your code you are using from android .
    – Monty
    Apr 29, 2013 at 4:06
  • I am trying to use text appearance provided by android.
    – Larry Lo
    Apr 29, 2013 at 4:16

2 Answers 2

223

Use like this. It will work.

textView.setTextAppearance(this, android.R.style.TextAppearance_Large);

Or, since API 23, you don't need to pass a context. Hence, you can simply call:

textView.setTextAppearance(android.R.style.TextAppearance_Large);

If you want to support API 23 or higher as well as lower one, you can use the below method to simplify your task. Use the below method only if you are already targeting API 23 or higher. If you are targeting API is less than 23, the below code will give error as the new method wasn't available in it.

public void setTextAppearance(Context context, int resId) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        super.setTextAppearance(context, resId);
    } else {
        super.setTextAppearance(resId);
    }
}
6
  • 3
    Thank you so much! It works ! when shall we use android.R.attr.textAppearanceLarge instead of android.R.style.TextAppearance_Large ?
    – Larry Lo
    Apr 29, 2013 at 4:32
  • @RajuGujarati: android.R.attr.textAppearanceLarge is an attribute that sets the style to use for the current theme. You can give it any value when you use a custom theme. android.R.style.TextAppearance_Large is a style, and in this case it happens to be the value set to the attr, for the default theme.
    – njzk2
    Jul 8, 2014 at 20:07
  • This method was deprecated in API level 23. Use setTextAppearance(int) instead in API 23+.
    – Sergii
    Nov 28, 2015 at 12:36
  • I get the following error on the lines with super.setTextAppea... "Cannot resolve method setTextAppearance(int)" Of course another solution would be not to monkeypatch setTextAppearance but it would be nice to get this working
    – Parsa
    Dec 6, 2015 at 19:28
  • 7
    @Arst mean TextViewCompat.setTextAppearance(@NonNull TextView textView, @StyleRes int resId) Dec 13, 2016 at 9:19
36

Use TextViewCompat.setTextAppearance() method which will take care of your sdk version checks.

3
  • How do you use this? I have the View.
    – doctorram
    Mar 30, 2018 at 22:48
  • @doctorram this is only for textview. If your view is textview then just use this method - TextViewCompat.setTextAppearance(your_text_view_here, R.style.yourStyle_which_want_to_apply); Apr 2, 2018 at 7:25
  • This is simpler than accepted answer
    – etomun
    Dec 9, 2021 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.