How to assign text size in sp value using java code

AndroidTextviewText Size

Android Problem Overview


If I assign an integer value to change a certain text size of a TextView using java code, the value is interpreted as pixel (px).

Now does anyone know how to assign it in sp?

Android Solutions


Solution 1 - Android

<http://developer.android.com/reference/android/widget/TextView.html#setTextSize%28int,%20float%29>

Example:

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 65);

Solution 2 - Android

Cleaner and more reusable approach is

define text size in dimens.xml file inside res/values/ directory:

</resources>
   <dimen name="text_medium">14sp</dimen>
</resources>

and then apply it to the TextView:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.text_medium));

Solution 3 - Android

You can use a DisplayMetrics object to help convert between pixels and scaled pixels with the scaledDensity attribute.

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
pixelSize = (int)scaledPixelSize * dm.scaledDensity; 

Solution 4 - Android

Based on the the source code of setTextSize:

public void setTextSize(int unit, float size) {
    Context c = getContext();
    Resources r;

    if (c == null)
        r = Resources.getSystem();
    else
        r = c.getResources();

    setRawTextSize(TypedValue.applyDimension(
        unit, size, r.getDisplayMetrics()));
}

I build this function for calulating any demension to pixels:

int getPixels(int unit, float size) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    return (int)TypedValue.applyDimension(unit, size, metrics);
}

Where unit is something like TypedValue.COMPLEX_UNIT_SP.

Solution 5 - Android

When the accepted answer doesn't work (for example when dealing with Paint) you can use:

float spTextSize = 12;
float textSize = spTextSize * getResources().getDisplayMetrics().scaledDensity;
textPaint.setTextSize(textSize);

Solution 6 - Android

By default setTextSize, without units work in SP (scales pixel)

public void setTextSize (float size) 
Added in API level 1
Set the default text size to the given value, interpreted as "scaled pixel" units. This 
size is adjusted based on the current density and user font size preference.

Solution 7 - Android

Thanks @John Leehey and @PeterH:

desiredSp = getResources().getDimension(R.dimen.desired_sp);
density = getResources().getDisplayMetrics().density;
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, desiredSp / density);

The thing is if you define R.dimen.desired_sp to 25 in your dimen.xml

  1. On non-HD Device: desiredSp is still 25, density = 1
  2. On HD Device(like Nexus 7 2nd Generation): desiredSp becomes 50 ish, density = 2

Solution 8 - Android

semeTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 
                       context.getResources().getDimension(R.dimen.text_size_in_dp))

Solution 9 - Android

This is code for the convert PX to SP format. 100% Works

view.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);

Solution 10 - Android

From Api level 1, you can use the public void setTextSize (float size) method.

From the documentation:

> Set the default text size to the given value, interpreted as "scaled > pixel" units. This size is adjusted based on the current density and > user font size preference. > > Parameters: > size -> float: The scaled pixel size.

So you can simple do:

textView.setTextSize(12); // your size in sp

Solution 11 - Android

After trying all the solutions and none giving acceptable results (maybe because I was working on a device with default very large fonts), the following worked for me (COMPLEX_UNIT_DIP = Device Independent Pixels):

textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncapecrawlerView Question on Stackoverflow
Solution 1 - AndroidSantoshView Answer on Stackoverflow
Solution 2 - AndroidklimatView Answer on Stackoverflow
Solution 3 - AndroidDave WebbView Answer on Stackoverflow
Solution 4 - AndroidrekireView Answer on Stackoverflow
Solution 5 - AndroidpomberView Answer on Stackoverflow
Solution 6 - AndroidZeus MonoliticsView Answer on Stackoverflow
Solution 7 - Androidmacio.JunView Answer on Stackoverflow
Solution 8 - AndroidWilliam HuView Answer on Stackoverflow
Solution 9 - AndroidKamran GasimovView Answer on Stackoverflow
Solution 10 - Androiduser2342558View Answer on Stackoverflow
Solution 11 - AndroidtsigView Answer on Stackoverflow