TextView FontSize according to various resolutions and ScreenSize

Here I am developing one Android application that can work on all devices with screen resolution and resolution. But one problem is that my TextView's Fontsize is the same on the whole screen. I want to change FontSize to suit different ScreenSize and screen resolution.

Thanks at Advance.

+6
source share
7 answers

Use code from Screen Category or use getSize () , e.g.

 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 then set the font size accordingly using the setTextSize () method, you can also consider using the sp block for the font size.

+5
source

First, if you haven’t done it yet, you should read it.

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

To provide any resource, including styles that may apply to text, you should read the Using Configuration Specifiers section.

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

sp

Scalable pixels are like a dp block, but it also scales according to the user's font size preference. We recommend that you use this machine when setting font sizes, so they will be adjusted for both screen density and user preferences.

Hope this helps.

+2
source

Hi, create the folders as shown below in the resources folder, and then copy your XML files there. Now you can check the palette window on which screens of different sizes will be displayed, based on which you can change the screen size.

layout is large, layout is small, layout is layout,

Now it supports all types of screen sizes, and the font size will be transparent based on the screen size. For more information on multi-screen support, check out the android documentation .

+1
source

automatically adjust the font size on the screen 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 the height of the screen is 854, its font size is 20.4 * /

to set the size in textview just use this code.

  textView.setTextSize(txtsize); 
+1
source

one way to do this is to make the necessary folders, such as the mock-up layout, the mock-up layout, the normal layout, the xlarge layout in the res folder. and put xmls in this folder and then change everything you want with a text view and everything

0
source

you should use sp block for font sizes instead of dip or dp . sp - scale-independent pixels that adjust to fit the pixel density of the screen. here is the exact difference.

dp

Density-independent pixels are an abstract block based on the physical density of the screen. These units refer to a screen with a resolution of 160 dpi, so one pixel is one pixel on a screen with a resolution of 160 dpi. The dp-to-pixel ratio will vary with screen density, but not necessarily in direct proportion. Note. The compiler accepts both "dip" and "dp", although "dp" more closely matches "sp".

sp
Scalable pixels - this is similar to the dp block, but it also scales according to the user's font size preference. It is recommended to use this machine when setting font sizes, so they will be configured both for screen density and user preferences.

0
source

Go to your XML file and add the text as:

 android:textSize="20sp" 

This will increase the size of your font.

-3
source

Source: https://habr.com/ru/post/915961/


All Articles