Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Different style (textSize) for different layout sizes

I have made custom style for all text buttons on large screens

<style name="button_large" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@color/button_color</item>
    <item name="android:textSize">30dp</item>
    <item name="android:clickable">true</item>
    <item name="android:soundEffectsEnabled">false</item>
</style>

Now i want smaller buttons for normal screen and I add noew style where only android:textSize changed.

<style name="button_normal" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@color/button_color</item>
    <item name="android:textSize">10dp</item>
    <item name="android:clickable">true</item>
    <item name="android:soundEffectsEnabled">false</item>
</style>

Is it posible to extract this value and recieve needed value based on screen size? And use only one style.

  <item name="android:textSize">value_based_on_screen_size</item>

And in case of large screen it weill be 30 and for normal screen it will be 10

like image 659
Liza Tjaica Avatar asked Jan 25 '13 18:01

Liza Tjaica


People also ask

Should I use SP or DP Android?

When setting text sizes, you should normally use sp , or "scale-independent pixels". This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

How do I stop system font size changing effects to Android application?

Prevent-system-font-size-changing-effects-to-android cs, override the Resources and set the configuration to default to restrict the font size effect on application. Resources. UpdateConfiguration() has been deprecated in API 25. So CreateConfigurationContext is used for APl level greater than 25.


2 Answers

Yes that's possible. Here's how you might arrange it. In your default values/styles.xml have,

<style name="button" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@color/button_color</item>
    <item name="android:textSize">@dimen/button_text_size</item>
    <item name="android:clickable">true</item>
    <item name="android:soundEffectsEnabled">false</item>
</style>

then create a values/dimens.xml like this,

<resources>
    <dimen name="button_text_size">10sp</dimen>
</resources>

and create a values-large like this,

<resources>
    <dimen name="button_text_size">30sp</dimen>
</resources>

P.s., use sp for fonts. it's just like dp, except that if the user increases the default font size through system settings, your fonts will change accordingly.

like image 158
Jeffrey Blattman Avatar answered Oct 20 '22 10:10

Jeffrey Blattman


Prepare such files first :

res/values-ldpi/dimens.xml
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml

Then declare resource values for different files:

<!-- in values-ldpi/dimens.xml -->
<dimen name="mytextSize">30dp</dimen>

and

<!-- in values-mdpi/dimens.xml -->
<dimen name="mytextSize">20dp</dimen>

Then, use this way in your style :

<item name="android:textSize">@dimen/mytextSize</item>
like image 20
Pratik Sharma Avatar answered Oct 20 '22 11:10

Pratik Sharma