TextView - setting the text size programmatically doesn't seem to work

Android

Android Problem Overview


I am using Eclipse Indigo, testing on 2 emulators(2.2 and 3.0).

the code below shows what I am testing now, however setting the text size reveals nothing on the screen when trying to run the emulator.(if i comment out the text size the text shows up with a red color). I thought that somehow eclipse wasn't rebuilding the code but i added the line of code to add the blue background and that worked. I have tried setting the text size after setting the text with still no success. the code is below. thanks for your help! (disclaimer) - i am trying to stay away from xml. Being that i already know java i don't want to depend on that.

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class TestAndroidvs2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView text = new TextView(this);
    text.setTextColor(Color.RED);
    text.setTextSize(2);    
    text.setBackgroundColor(Color.BLUE);
    text.setText("Hello Android");
 
    
    setContentView(text);
  }
}

Android Solutions


Solution 1 - Android

the method TextView.setTextSize(int unit , float size); takes two parameters .

Try this :

text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);

refer this and this.

UPDATE: Now the setTextSize(float size) will set the text size automatically in "scaled pixel" units. no need to mention the COMPLEX_UNIT_SP manually. Refer to the documentation.

Solution 2 - Android

This fixed the issue for me. I got uniform font size across all devices.

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

Solution 3 - Android

Currently, setTextSize(float size) method will work well so we don't need to use another method for change text size

android.widget.TextView.java source code

/**
 * 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.
 *
 * <p>Note: if this TextView has the auto-size feature enabled than this function is no-op.
 *
 * @param size The scaled pixel size.
 *
 * @attr ref android.R.styleable#TextView_textSize
 */
@android.view.RemotableViewMethod
public void setTextSize(float size) {
    setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}

Example using

textView.setTextSize(20); // set your text size = 20sp

Solution 4 - Android

Text size 2 will be practically invisible. Try it with 14 at least. BTW, using xml has a lot of advantages and will make your life easier once you need to do anything more complex than 'Hello World'.

Solution 5 - Android

Please see this link for more information on setting the text size in code. Basically it says:

> public void setTextSize (int unit, float size) > > Since: API Level 1 Set the default text size to a given unit and > value. See TypedValue for the possible dimension units. Related XML > Attributes > > android:textSize Parameters > > unit The desired dimension unit.
> size The desired size in the given units.

Solution 6 - Android

In My Case Used this Method:

public static float pxFromDp(float dp, Context mContext) {
    return dp * mContext.getResources().getDisplayMetrics().density;
}

Here Set TextView's TextSize Programatically :

textView.setTextSize(pxFromDp(18, YourActivity.this));

Keep Enjoying:)

Solution 7 - Android

In Kotlin, you can use simply use like this,

textview.textSize = 20f

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
QuestioncspamView Question on Stackoverflow
Solution 1 - AndroidHoucineView Answer on Stackoverflow
Solution 2 - AndroidVikasView Answer on Stackoverflow
Solution 3 - AndroidLinhView Answer on Stackoverflow
Solution 4 - AndroidNikolay ElenkovView Answer on Stackoverflow
Solution 5 - AndroidJackView Answer on Stackoverflow
Solution 6 - AndroidDhruv RavalView Answer on Stackoverflow
Solution 7 - AndroidAhamedView Answer on Stackoverflow