hasanghaforian
hasanghaforian

Reputation: 14022

Change TextAppearance of all text of application programmatically

In an Android application I have 24 themes (12 themes for day, 12 themes for night), for example Blue, Black, ... and user can change application theme at runtime. Now I want to enable user to change global text size. Text size may be small, medium and large. A way for doing this is creating 3 distinct themes instead of Blue: Blue.Small, Blue.Medium and Blue.Large and so on for other themes. But this cause creating 72 themes! Is there a shorter way for doing this? For example changing global TextAppearance of current theme programmatically?

Upvotes: 0

Views: 525

Answers (1)

emandt
emandt

Reputation: 2706

It's difficult to hel you if we don't see some code.

However I'm doing this:

<style name="TextAppearance.Application.Small" parent="@style/TextAppearance.AppCompat.Small">
    <item name="android:textSize">20sp</item>
</style>
<style name="TextAppearance.Application.Medium" parent="@style/TextAppearance.AppCompat.Medium">
    <item name="android:textSize">25sp</item>
</style>
<style name="TextAppearance.Application.Large" parent="@style/TextAppearance.AppCompat.Large">
    <item name="android:textSize">30sp</item>
</style>

<style name="TextViewStyle.Application" parent="@style/Widget.AppCompat.TextView">
    <item name="android:textAppearanceSmall">@style/TextAppearance.Application.Small</item>
    <item name="android:textAppearanceMedium">@style/TextAppearance.Application.Medium</item>
    <item name="android:textAppearanceLarge">@style/TextAppearance.Application.Large</item>
    <!-- default if no any specific Appearance is set -->
    <item name="android:textAppearance">@style/TextAppearance.Application.Small</item>
</style>

Then in the Theme:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textAppearance">@style/TextViewStyle.Application</item>
</style>

Upvotes: 1

Related Questions