Set text size with dimens - Android studio

202 views Asked by At

I want to change text size by screen resolution to phones and i used with dimens like this:

enter image description here

I set by Screen Height and its works fine with phone like naxus 5 or pixel 3XL. My question is if this is how I should define? What did I do well enough to fit any cell phone and tablet?

1

There are 1 answers

0
Nathan Walsh On

The files listed above will most certainly cover any android device or tablet. You can always add more configurations if you eventually find a device that isn't utilizing the correct dimens.xml that you desire. There are so many android devices of all shapes and sizes, it is hard to have a coverall approach by using xml files.

Another option would be to programmatically set the the font size based on a ratio of the screen dimensions which can be found below. Exactly how to best do that would depend on how your app is set up.

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

Using the height and width above you could change the font size. However, Where exactly to do that in your code depends entirely on how your app is set up.