Android: How do I set the textsize for a layout?

23,288

To set the global styling for all TextViews, set the android:textViewStyle attribute in a custom theme and apply the theme to your application or individual activities.

Pseudocode:

<style name="MyTheme" parent="android:Theme">
  <item name="android:textViewStyle">@style/MyTextView</item>
</style>

<style name="MyTextView" parent="android:Widget.TextView">
  <item name="android:textSize">14sp</item>
</style>

...

<activity android:theme="@style/MyTheme">
  ...
Share:
23,288
k_man
Author by

k_man

Kind of %FILL_IN_THE_BLANK%.

Updated on June 02, 2020

Comments

  • k_man
    k_man almost 4 years

    I am trying to set the textsize at a global level and cannot figure or find a way to do it.

    For example I have something like this:

    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id = "@+id/Table"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:shrinkColumns="*"  
             android:stretchColumns="*">
    </TableLayout>
    </ScrollView>
    

    The TableRows and TextViews themselves are generated dynamically depending on user input.

    The problem is I would like to set the base textSize globally based on a resource file without having to go and touch a bunch of code. Is there a way to tell the app, "hey app, use this as your base textSize for all the textViews?"

    Or phrased another way, in HTML I can set the font size at the body, div, and table levels. Is there something analogous I can do at one the layout (LinearLayout, TableLayout, ScrollView, etc.) levels?

  • k_man
    k_man over 12 years
    thanks that did the trick. I googled for hours and could not find it. Thanks again.
  • Petr Prazak
    Petr Prazak about 12 years
    Also worth a mention that "you should prefer the sp (scale-independent pixel) to define text sizes. The sp scale factor depends on a user setting and the system scales the size the same as it does for dp." developer.android.com/guide/practices/…
  • Walt Armour
    Walt Armour about 11 years
    Finally! We've had text style pain for ages because textAppearance would not inherit/cascade properly (groups.google.com/d/msg/android-developers/KJfM0IhFRrk/…). Using this attribute instead does the trick.
  • Walt Armour
    Walt Armour about 11 years
    And hours later I realize this answer was not the one I wanted. Using textViewStyle will set the properties but will also prevent local override of those values (by setting textAppearance directly on a TextView). The real solution: do not set android:textAppearance on your theme, set android:textAppearanceSmall! The internals use that latter attribute to setup the styles for default child textviews. This also then allows for local override of the style using android:textAppearance.