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
-
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 Answers
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);

- 1,487
- 1
- 18
- 34
-
2I tried this for `Canvas.setTextSize()`. It does help, though the text is not the same size on the 2 devices on which I tested. – hBrent Sep 18 '14 at 22:01
-
-
but isn't the density in dp? it likes setting the text size in xml to 16dp which isn't right i think – has19 Jul 22 '16 at 12:08
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"

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

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

- 74,896
- 15
- 165
- 198

- 2,083
- 2
- 17
- 23
(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;
}

- 2,624
- 3
- 21
- 34
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
-
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
-
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);

- 878
- 1
- 8
- 8
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);
}

- 11
- 1
for textSize, google suggested using 'sp' instead of 'dp'
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

- 560
- 3
- 11
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.

- 513
- 6
- 16
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);

- 21
- 1