Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set style or font to text of a TextView in android?

I want to set style or font to the text in a TextView like the image shown below:

enter image description here

like image 705
The iCoder Avatar asked Feb 27 '13 13:02

The iCoder


People also ask

Which method is used to set the text in a TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method.

How do I get TextView text?

String a = tv. getText(). toString(); int A = Integer. parseInt(a);


3 Answers

<TextView
style="@style/CodeFont"
android:text="@string/hello" />

You need to Make that codefont style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

Straight from : http://developer.android.com/guide/topics/ui/themes.html

like image 152
Gjordis Avatar answered Nov 19 '22 11:11

Gjordis


You need a custom font and then you can do this:

Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
MyTextView.setTypeface(mFont);

You have to create a "fonts" folder in your assets folder. Drop your font in there.
You could also create a custom TextView of course. Refer to this answer, I gave a while back, if you prefer that.

like image 28
Ahmad Avatar answered Nov 19 '22 11:11

Ahmad


There is another way if you want to change it on many TextViews, Use a class:

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyTextView(Context context) {
    super(context);
    init();
}

private void init() {
    if (!isInEditMode()) {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Ubuntu-L.ttf");
        setTypeface(tf);
    }
}

}

and in the Layout replace:

<TextView 
...
/>

With:

<com.WHERE_YOUR_CLASS_IS.MyTextView 
...

/>
like image 22
PaperThick Avatar answered Nov 19 '22 11:11

PaperThick