0

I'm currently creating TextView like that:

LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.ll1);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,    LinearLayout.LayoutParams.WRAP_CONTENT);

for(int l=0; l<4; l++)
    {
        pairs[l] = new TextView(context);
        pairs[l].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        pairs[l].setLayoutParams(lp);
        pairs[l].setId(l);
        pairs[l].setText("asd");
        myLayout.addView(pairs[l]);
    }

Now I want set this attribute to all of this TextView:

  • FontFamily: cursive
  • SetTextSize not sp but dp (RESOLVED)
  • SetGravity: central_horizontal (RESOLVED)

I couldn't find a way for set those attribute when I create a TextView programatically, How can I do that?

GMX
  • 950
  • 1
  • 14
  • 29

3 Answers3

2

Text size in dp can be set using setTextSize(TypedValue.COMPLEX_UNIT_DIP, <float>) - see documentation [here](https://developer.android.com/reference/android/widget/TextView.html#setTextSize(int, float)).

As for the font family, I'm afraid I don't know - hopefully someone else can help you with this :)

clownba0t
  • 1,103
  • 10
  • 12
1

In order to change the font family of a TextView use setTypeface

for instance:

Typeface tf = Typeface.create("cursive", Typeface.NORMAL);
for(int l=0; l<4; l++)
{
    pairs[l] = new TextView(context);
    pairs[l].setTypeface(tf);
    ...
}

Also, this may interest: How to change fontFamily of TextView in Android

Community
  • 1
  • 1
0

You can set the font using Typeface object.

TextView tv = new TextView(context);
Typeface face = Typeface.createFromAsset(getAssets(),
       "fonts/filename.ttf");
tv.setTypeface(face);

Put the font in the assets folder of your project.

skynet
  • 706
  • 4
  • 8