Text size with different resolution

67,825

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:

  1. Create file dimens.xml in values directory.
  2. Put there <dimen name="font_size">30sp</dimen>. This is default font size.
  3. Create file dimens.xml in values-large directory.
  4. Put there <dimen name="font_size">20sp</dimen>. This is font size for galaxy tab 1.
  5. Create file dimens.xml in values-sw600dp directory.
  6. Put there <dimen name="font_size">30sp</dimen>. This is font size for other tablets with Android 3.x and newer.
  7. 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...

Multiple Screen Support

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.

Share:
67,825

Related videos on Youtube

colymore
Author by

colymore

Im from spain, learning to code android apps, and soon wp7 and iOS

Updated on October 20, 2020

Comments

  • colymore
    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

  • colymore
    colymore almost 12 years
    Yes but the 2 tablets use large layouts but with diferent resolution, then i will have same problem.
  • karn
    karn almost 12 years
    you can use dip for text also but recommended unit is sp. Changing from sp to dip will only make the situation worse....
  • karn
    karn almost 12 years
    why 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
    vasart almost 12 years
    Because Galaxy Tab 1 is MDPI, but scales text to HDPI size
  • karn
    karn almost 12 years
    size 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
    vasart almost 12 years
    values-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
    vasart almost 12 years
    Yep, 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 from values-large. Small or medium screen devices with any api level will use default font size from values. That's why we need these three folders.
  • Meng Zhang
    Meng Zhang about 10 years
    textSize, not textsize
  • Sotti
    Sotti almost 10 years
    textSize is always SP, not DP. Google made it clear.
  • hBrent
    hBrent almost 10 years
    I 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
    M. Usman Khan over 8 years
    Doesn't work if the screen size is big but the density is low.
  • Guihgo
    Guihgo over 8 years
    this is the best way !
  • has19
    has19 almost 8 years
    but isn't the density in dp? it likes setting the text size in xml to 16dp which isn't right i think
  • tux-world
    tux-world about 7 years
    Call requires API level 17 for display.getRealSize(size);