Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 846
k_man Avatar asked Dec 05 '11 01:12

k_man


People also ask

What is default text size in Android?

You should think of this as the normal font size, and basically everything else a variation on it. For instance, while 14sp is the default text size when the text can be quite long, when there's only a small modal with a bit of text, it's 16sp! Notice that it's a bit lighter to make up for this size boost.


1 Answers

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">
  ...
like image 179
Roman Nurik Avatar answered Oct 22 '22 05:10

Roman Nurik