TextView FontSize according to Different Resolution and ScreenSize

13,796

Solution 1

Use the code from Screen Category or use getSize() method like:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

as described here to get the screen size and and then set the font size accordingly using setTextSize() method, you can also consider using sp unit for font size.

Solution 2

First, if you have not done so already, you should read this

http://developer.android.com/guide/practices/screens_support.html

To provide any resource, including styles that could apply to text, you should read the section Using configuration qualifiers

Another useful document here http://developer.android.com/guide/topics/resources/more-resources.html#Dimension should help you with selecting the right unit of measure for text, ideally you want to use sp's as explained in the excerpt:

sp

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.

Hope that helps.

Solution 3

Hi create the folders as below in your resources folder and then copy your XML files in to it now you can check the palette window it will show the screens for different sizes based on that u can modify the screen size .

layout-large, layout-small, layout-xlarge ,

Now it supports to all types of screen sizes and your Font size will be clear based on screen size. For more information regarding to Supporting Multiple Screens check the android documentation .

Solution 4

automatic adjust font size as per screen by using this code

Display display;
Point size;
int width, height;
float txtsize;

declare and use in oncreate()

display = getWindowManager().getDefaultDisplay();
         size = new Point();
        display.getSize(size);
         width = size.x;
         height = size.y;
         txtsize=height*0.024f;

/* if your screen height is 854 its use font size 20.4*/

to set size to textview just use this code..

 textView.setTextSize(txtsize);
Share:
13,796
Keyur Android
Author by

Keyur Android

Updated on June 04, 2022

Comments

  • Keyur Android
    Keyur Android about 2 years

    Here i develop one Android application, which can run on all screen size and resolution devices. But one problem is there my TextView's Fontsize is same on all the Screen-Size. I want to change FontSize according to Different ScreenSize and Screen Resolution.

    Thanks in Advance.