view raw
GMX GMX - 6 months ago 37
Android Question

How create TextView programatically with this specifics Attributes?

I'm currently creating TextView like that:

LinearLayout myLayout = (LinearLayout) activity.findViewById(;
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);

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?


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

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