-3

How to set the size of the text in text view based on the screen size and to scale dynamically based on height and width of the screen without creating different folders based on the resolution in android.

Thanks in advance

sushh
  • 115
  • 1
  • 10
  • I was able to solve the issue succesfully by creating the folders for different resolution and setting the font size. I created separate files under values section under dimens.xml, the different dimension files are sw320dp, sw480dp, sw600dp, sw720dp and normal file. Hope this helps sme1. – sushh Oct 07 '16 at 06:49

2 Answers2

1

This question will help you get your screen dimensions in pixels.

https://stackoverflow.com/a/1016941/6286125

You can do some basic math to adjust your text as you wish afterward.

EDIT : As I already used DisplayMetrics, I would use it like this :

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int myTextSize = metrics.widthPixels / 10; 
textView.setTextSize(myTextSize);

Then you can adjust as you wish, using display's width or height.

In case you are not doing this in an activity, you need to get your activity first in order to use "getWindowManager()".

I would recommend to check which of width or height is bigger in order to know if you are in portrait or in landscape and adapt your math to this parameter :

int myWidth = metrics.widthPixels;
int myHeight = metrics.heightPixels;

if(myWidth > myHeight) { //Landscape
    myTextSize = myWidth / 10;
} else { //Portrait
    myTextSize = myHeight / 10;
}
Community
  • 1
  • 1
Cedric
  • 149
  • 1
  • 3
  • 12
0

I suggest you to use sp for text sizes to support multiple screens.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
  • I tried using sp but it didnt solve the issue . So i created different folders for different resolution and assigned the values accordingly. – sushh Oct 26 '16 at 09:00