Text size with different resolution
Solution 1
This should be some help for you if you want to set size programmatically. Text will show in the same size on each device
TextView text = new TextView(this);
text.setText("text");
text.setTextSize(16 * getResources().getDisplayMetrics().density);
Solution 2
By hardware specifications Galaxy Tab 1 is MDPI device, but because it uses Android 2.x Samsung set it programmatically to use HDPI resources. So I can advice you to make following:
- Create file
dimens.xml
invalues
directory. - Put there
<dimen name="font_size">30sp</dimen>
. This is default font size. - Create file
dimens.xml
invalues-large
directory. - Put there
<dimen name="font_size">20sp</dimen>
. This is font size for galaxy tab 1. - Create file
dimens.xml
invalues-sw600dp
directory. - Put there
<dimen name="font_size">30sp</dimen>
. This is font size for other tablets with Android 3.x and newer. - In layout specify
android:textSize="@dimens/font_size"
Solution 3
you just need to create different folders related to density or screen size like
Option 1.
values-large
values-small
values-normal
For more explanation check this link...
Option 2.
mTextView.setTextSize(16 * getResources().getDisplayMetrics().density);
this will give TextSize depending on density..
Solution 4
(1)I think using "dp" or "dip" for text is better than using "sp" because dp(density pixel) adjusts according to screen density. So text will look bigger on High-density devices and smaller on low-density devices.
(2) OR if you specifically want to use "sp" for text size, then you can detect the device density and then set the size of text accordingly:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
//set text-size for low-density devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
//set text-size for medium-density devices.
break;
case DisplayMetrics.DENSITY_HIGH:
//set text-size for high-density devices.
break;
}
Solution 5
try this:
like this
android:textSize= "10sp"
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.
Related videos on Youtube
colymore
Im from spain, learning to code android apps, and soon wp7 and iOS
Updated on October 20, 2020Comments
-
colymore over 3 years
i am using a galaxy tab 1, 7inch hdpi and a galaxy tab plus, 7 inch hdpi but more resolution, and in my application, the text can be read fine in galaxy tab but in galaxy tab plus there are too much small. Im using sp in font size and layout-large. Any idea? Thanks
-
Bhavesh Jethani almost 10 yearsmay be you can find answer from here also stackoverflow.com/questions/16706076/…
-
-
colymore almost 12 yearsYes but the 2 tablets use large layouts but with diferent resolution, then i will have same problem.
-
karn almost 12 yearsyou can use dip for text also but recommended unit is sp. Changing from sp to dip will only make the situation worse....
-
karn almost 12 yearswhy do you need to put the text dimension in different values folder?? Android treats the default values folder as one for the mdpi device and scale it based on current device configuration. If device is in large category then it will scale it. Correct me if I am wrong.
-
vasart almost 12 yearsBecause Galaxy Tab 1 is MDPI, but scales text to HDPI size
-
karn almost 12 yearssize and resolution are two different things. A small size device can lie in the hdpi category and a large device in the ldpi category. So how using values-large and values-small is going to be of any help here??
-
vasart almost 12 yearsvalues-large will be used only by tablets with Android 2.x, which has this scaling problem, any other large screen device will use values-sw600dp
-
vasart almost 12 yearsYep, that means any large or xlarge screen devices with api level >= 11 will use
values-sw600dp
with default font size. Large screen devices with api < 11 will use smaller font size fromvalues-large
. Small or medium screen devices with any api level will use default font size fromvalues
. That's why we need these three folders. -
Meng Zhang about 10 yearstextSize, not textsize
-
Sotti almost 10 yearstextSize is always SP, not DP. Google made it clear.
-
hBrent almost 10 yearsI tried this for
Canvas.setTextSize()
. It does help, though the text is not the same size on the 2 devices on which I tested. -
M. Usman Khan over 8 yearsDoesn't work if the screen size is big but the density is low.
-
Guihgo over 8 yearsthis is the best way !
-
has19 almost 8 yearsbut isn't the density in dp? it likes setting the text size in xml to 16dp which isn't right i think
-
tux-world about 7 yearsCall requires API level 17 for
display.getRealSize(size);