Zeeno
Zeeno

Reputation: 2721

How can I set the font of a textView in Android?

I'm trying to set the font to a custom font. the font is in a subfolder of the assets folder called "fonts" . This is code in my onCreate() function. Eclipse suddendly brings up the debugger saying "source not found"

    Typeface centuryGothic = Typeface.createFromAsset(this.getAssets(), "fonts/Century_Gothic_Bold.ttf");
    TextView tv = (TextView) findViewById(R.id.TitleAct_title);
    tv.setTypeface(centuryGothic);
    setContentView(tv);

This is the xml in My main at the moment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">

    <TextView
    android:text="@string/TitleAct_title"
    android:id="@+id/TitleAct_title"
    android:textColor="#fff"
    android:gravity="center_horizontal"
    android:background="#347"
    android:textSize="15pt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

 </LinearLayout>    
</LinearLayout>

I can't see anything wrong with this. Why is it bringing up the debugger and error "source not found"?

Upvotes: 1

Views: 741

Answers (1)

MByD
MByD

Reputation: 137442

What you saw was the eclipse debugger looking for the source of the exception. next time, press continue and look ad the log of the exception in the Logcat window.

You should call setContentView(R.layout.main); before TextView tv = (TextView) findViewById(R.id.TitleAct_title);, and probably drop the call setContentView(tv);.

Upvotes: 1

Related Questions