15
 layout=(RelativeLayout)findViewById(R.id.relate);
                TextView tv = new TextView(DetailActivity.this);
                layout.addView(tv);
                tv.setTextAppearance(DetailActivity.this, android.R.style.TextAppearance_Medium);
                tv.setGravity(Gravity.CENTER_HORIZONTAL);
                tv.setText("Hello Man");

Here is my code how to use setTextAppearance for API > 23

Help Appreciated

Jitesh Lakhwani
  • 305
  • 1
  • 2
  • 8

4 Answers4

58

Use TextViewCompat from support library:

TextViewCompat.setTextAppearance(tv, android.R.style.TextAppearance_Medium);
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
3
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                titleTextView.setTextAppearance(R.style.TextAppearance_MaterialComponents_Headline6);
            } else {
                titleTextView.setTextAppearance(context, R.style.TextAppearance_MaterialComponents_Headline6);
            }
Sourav Bagchi
  • 656
  • 7
  • 13
1

For API > 23, do :

tv.setTextAppearance(android.R.style.TextAppearance_Medium);
Christian
  • 748
  • 7
  • 23
  • If you support API < 23 (as most devs do at the moment) the code gets messy as you'll have put loads of `if (SDK >= 23)` - best to use `TextViewCompat.setTextAppearance(view, resId)` – Zain Jan 27 '17 at 12:07
0

it means you don't need to put the context arg there you can simply put the style id. it gets context automatically now

Jon halls
  • 44
  • 6
  • 1
    Only from API >=23 - if you support below this SDK you'll have litter your code with `if (SDK >= 23)` - best to use `TextViewCompat.setTextAppearance(view, resId)` – Zain Jan 27 '17 at 12:09