TextView - setting text size does not work programmatically

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

The code below shows that I'm testing now, however setting the text size does not show anything on the screen when trying to start the emulator. (if I comment on the size of the text, the text is displayed in red). I thought somehow the eclipse did not rearrange the code, but I added a line of code to add a blue background, and that worked. I tried to set the size of the text after the text was still unsuccessful. code below. Thanks for your help! (disclaimer) - I try to stay away from xml. Being what I already know java, I don't want to depend on it.

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); } } 
+124
android
Aug 09 '11 at 15:43
source share
6 answers

Text size 2 will be virtually invisible. Try at least 14. BTW, using xml has many advantages and will make your life easier when you need to do something more complex than Hello World.

+17
Aug 09 '11 at 15:51
source share

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

Try the following:

 text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14); 

send this and this .

+467
Aug 09 '11 at 16:00
source share

This solved the problem for me. I have the same font size on all devices.

  textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.font)); 
+66
Sep 09 '15 at 3:56
source share

Currently, the setTextSize(float size) method will work well, so we don’t need to use another method to resize the text

 /** * 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 
+22
Nov 17 '17 at 1:52
source share

For more information on adjusting the text size in the code, see this link. It basically says:

public void setTextSize (int unit, float size)

Because: API Level 1 Set the default text size for this device and cost. See TypedValue for possible units. Related XML Attributes

Android options: textSize

unit Required unit of measure.
size Desired size in given units.

+12
Aug 09 '11 at 15:48
source share

In my case, this method is used :

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

Here's the Set TextView TextSize Programatically :

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

Keep using :)

+9
Mar 02 '15 at 12:46
source share



All Articles