views:

193

answers:

2

hi all, How to set font to textview, which is created at runtime,?

i created tectview Textview tv = new TextView(this);
tv.setTextSize(20);

like Size i want to set font style to "Verdana".

How to do this?? regards shishir

A: 

You need to use Typeface. Look here for links and examples

Asahi
+2  A: 

To set In-built Font at Run-Time:

  • First of all, To Change Font-face, a Typeface class is used.

  • Now, at Run-Time, to set the font-face, Use setTypeface(Typeface) from the Java code

  • at Design-Time, to set the font-face, Use android:typeface="serif"

For example:

<TextView android:text="@+id/TextView01"
 android:id="@+id/TextView01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="30px"
 android:textStyle="italic"
 android:typeface="serif" />

To set Custom (User-desired) font(s) with your application

To do this, simply create an assets/ folder in the project root, and put your fonts (in TrueType, or TTF, form) in the assets. You might, for example, create assets/fonts/ and put your TTF files in there:

  TextView tv=(TextView)findViewById(R.id.custom); 
  Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); 
  tv.setTypeface(face); 
PM - Paresh Mayani