1

I'm trying to have my own font in a listview using Java in Android OS 2.3.x

After reading the following links, i'm stuck:

http://developer.android.com/guide/topics/ui/themes.html http://stackoverflow.com/questions/4576441/custom-font-in-android-listview http://androidforums.com/android-lounge/143874-custom-typface-listview-row-layout.html

i can't post something usefull here.

My main problems are:

  • Why can't i change the Font for a listview, using setTypeface?
  • Why can't i define a Font for ALL texts in my application without putting it in every activity again?
  • Is there a documentation, which handles this problems? The Android SDK documentation is lacking a huge amount of details, like at which version otf Fonts are working.

I know that i have to learn many things and i'm ready as much books about this topics as i can. But after two days guessing and trying around, i have to ask for help.

Propably someone can push me in the right direction ;)

To give you a small idea, i tried to clean up some code:

File: res/layout/scores.xml (snippet from it)

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:cacheColorHint="#00000000"
    android:transcriptMode="disabled" >
</ListView>

File: src/notneededasinfo/ScoresActivity.java (snippet from it)

Resources myResources = getResources();

Typeface tf = Typeface.createFromAsset(myResources.getAssets(),"fonts/searstower.ttf");
TextView tv = (TextView) findViewById(android.R.id.list);
tv.setTypeface(tf);

Thx for help!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
MrMarco
  • 63
  • 1
  • 5

3 Answers3

1

Well... first of all android:textStyle and setTypeface(Typeface) are not a property and function from ListView, it is from TextView.

Try to use this tutorial: http://developer.android.com/resources/tutorials/views/hello-listview.html

Anyway, you have to define a .xml file that defines the style for the itens in the ListView. At the link I posted before you can see on topic 4 the handler onCreate the following line:

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

So, you have to define the layout for the ListView item on the file list_item.xml used above as R.layout.list_item. On this file, list_item.xml, you define new TextViews or EditTexts or whatever and changes the fonts individualy for each View.

Hope it helps you...

trebew
  • 121
  • 2
  • 2
  • 9
  • 1
    And other thing... your R.id.list is a ListView, as defined in your posted xml code. You cannot associate it with a TextView like you did here: TextView tv = (TextView) findViewById(android.R.id.list); – trebew Mar 02 '12 at 10:53
  • AAHH... That might be my problem. I thought that the ListView is a normal widget. But that it is only a container for the result lines... ok... give me some time. I know have something to work on. Merci! – MrMarco Mar 02 '12 at 11:27
  • I tried it, but you can't set a own font in the xml file. You can change things like font color, font style (bold etc.) and font size, but not font type. I'm missing something extremly important as knowledge. As soon as i got what i miss, i come back to this question. – MrMarco Mar 02 '12 at 12:19
0
TextView tv = (TextView) findViewById(android.R.id.list);

You need to have a text view to set the font. You are missing the list row view.

Tarun
  • 13,727
  • 8
  • 42
  • 57
0

I have a answer for one of your questions

Why can't i define a Font for ALL texts in my application without putting it in every activity again?

This is a known problem but easy to solve. Just implement a custom activity class which extends Activity and overwrite the setContentView() method. In this Method you can get all your TextViews for your current view and set the Typface

See here: Add custom font for complete android application

Community
  • 1
  • 1
Henry
  • 816
  • 5
  • 17