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?

2
  • 2
    use layout params to adjust those Commented Nov 11, 2016 at 17:28
  • I did it for adjust gravity and the height (wrap_content), but How about last two attributes? Commented Nov 11, 2016 at 17:33

3 Answers 3

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 :)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

1 Comment

Can't use this because i don't have the file .TFF of "Cursive"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.