Bincy Baby
Bincy Baby

Reputation: 4743

TextSize for different device with different size?

How can support text size for different devices with different screen size ? I have try to provide different dimension xml for different screen but it's not working efficiently.

values values-large values-small values-sw600dp values-sw720dp values-sw800dp

Upvotes: 1

Views: 392

Answers (2)

Sash_KP
Sash_KP

Reputation: 5591

Your approach is accurate when you have values values-large values-small values-sw600dp values-sw720dp values-sw800dp etc folders. However i think you are missing to change textsize dimensions in values/dimens.xml values-large/dimens.xml values-small/dimens.xml values-sw600dp/dimens.xml values-sw720dp/dimens.xml values-sw800dp/dimens.xml.

For example you have used something like below dimens.xml in your values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18sp</dimen>
</resources>  

In other values folder you need to change values for your text size.Also use sp for textsize not px or dp.

Please note that values values-large values-small are depricated after honeycomb.

You can also use the library as suggested in the other answer.However if you don't want to use an extra library then try this.

Programmatically

Here's a method which converts size of dp to the size according to screen size.

public float convertFromDp(int input) {
final float scale = getResources().getDisplayMetrics().density;
return ((input - 0.5f) / scale);
} 

Now simply give the value to the textsize programically.

tvTextView1.setTextSize(convertFromDp(24));

Upvotes: 2

Aerrow
Aerrow

Reputation: 12134

Try this library, it may help you. CommonTextSize

In your layout add like this to mention size android:textSize="@dimen/_15sdp"

Upvotes: 2

Related Questions