7

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

enter image description here

God
  • 1,238
  • 2
  • 18
  • 45
The iCoder
  • 1,414
  • 3
  • 19
  • 39

4 Answers4

12
<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

Gjordis
  • 2,540
  • 1
  • 22
  • 32
11

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.

Community
  • 1
  • 1
Ahmad
  • 69,608
  • 17
  • 111
  • 137
3

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 
...

/>
PaperThick
  • 2,749
  • 4
  • 24
  • 42
0

You could create a layout.xml file that would have your textview in it. Something like :

textView.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
style="@android:style/Holo.ButtonBar" >

If you dont want this then you could create your custom style. Something like this :

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Custom" parent="@android:style/TextAppearance.Large" >
        <item name="android:typeface">monospace</item>
    </style>
</resources>

and in the layout file change the style to something like :

style="@style/Custom"
lokoko
  • 5,785
  • 5
  • 35
  • 68