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
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
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;
}
I suggest you to use sp for text sizes to support multiple screens.