17

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
  • 11,776
  • 13
  • 48
  • 90
  • may be you can find answer from here also http://stackoverflow.com/questions/16706076/font-size-and-images-for-different-devices – Bhavesh Jethani Jul 24 '14 at 08:10

12 Answers12

34

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);
Dr Glass
  • 1,487
  • 1
  • 18
  • 34
19

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"
vasart
  • 6,692
  • 38
  • 39
  • 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. – karn Sep 27 '12 at 12:07
  • Because Galaxy Tab 1 is MDPI, but scales text to HDPI size – vasart Sep 27 '12 at 12:08
  • 1
    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?? – karn Sep 27 '12 at 12:19
  • 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 Sep 27 '12 at 12:23
  • 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. – vasart Sep 27 '12 at 12:38
  • this is the best way ! – Guihgo Feb 27 '16 at 05:23
16

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..

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
  • Yes but the 2 tablets use large layouts but with diferent resolution, then i will have same problem. – colymore Sep 27 '12 at 11:58
4

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.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23
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; 
    } 
Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34
3

Better to use different values, Please have a look -

Different values folders in android

for more take a look

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

basics of device-independent-pixels

OR

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

Community
  • 1
  • 1
bashu
  • 1,710
  • 12
  • 16
  • you can use dip for text also but recommended unit is sp. Changing from sp to dip will only make the situation worse.... – karn Sep 27 '12 at 12:03
  • textSize is always SP, not DP. Google made it clear. – Sotti Aug 07 '14 at 14:57
2

You can use any of this dimensions, that market as based on the physical size of screen. Than texts on all devices will have the same physical size.

Jin35
  • 8,602
  • 3
  • 32
  • 52
2

The below code adjusts font size scaling in a textView, using the User's system font size setting.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
int screenWidth = size.x;
float fontscale = getResources().getConfiguration().fontScale;
float textSize = (screenWidth/ fontscale)/10;
Log.e(TAG, "textSize is:  " + textSize);
yourTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,  textSize);
Rick
  • 878
  • 1
  • 8
  • 8
1

TextView headerTextView(String label){

    TextView headerTextView = new TextView(this.context);
    float scale = context.getResources().getDisplayMetrics().density;
    if(scale > 2.0) {
    headerTextView.setWidth(210);
    headerTextView.setHeight(120);
    headerTextView.setTextSize(17);
    }
    else {
        headerTextView.setWidth(125);
        headerTextView.setHeight(60);
        headerTextView.setTextSize(21);
    }
yug
  • 11
  • 1
0

for textSize, google suggested using 'sp' instead of 'dp' enter image description here

chcek this link http://developer.android.com/design/style/typography.html

and for supporting font size for multiple screen you should create new resource for each screen size http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

Fahriyal Afif
  • 560
  • 3
  • 11
0

In short, you can make use of scalable DP and scalable Font approach.

Android project structure inherently allows specifying different font size (SP) and margin values (DP) by defining them in dimens.xml files for supported screen sizes. But the problem that still remain is - what values to be used ? UX designers normally provide style guides for only one set (e.g. 360x640 DP screen size) and don't provide for all other supported screen sizes. So the problem to use right vlaues for other supported screen sizes still remain unanswered.

One good solution (per my understading) is to use readymade scalable dictionary of dimens.xml files, something similar to the dictionary of files provided by this library. After adding these xml files to project, you just need to use the keys in your layouts, then Android automatically uses font/margin value from respective directory. Please refer here for visual difference - with and without this approach.

Birender Singh
  • 513
  • 6
  • 16
0

TextView tv = new TextView(context); tv.setText("Hello World");

// Value is a scaled pixel (SP) tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);

// Value is Device Independent Pixels (DIP) tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20f);

// Value is raw pixels (PX) tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f);

// Value is in points (PT) tv.setTextSize(TypedValue.COMPLEX_UNIT_PT, 20f);

// Value is in millimeters (MM) tv.setTextSize(TypedValue.COMPLEX_UNIT_MM, 20f);

// Value is in inches (IN) tv.setTextSize(TypedValue.COMPLEX_UNIT_IN, 20f);

Darkman
  • 21
  • 1