4

I can't set a font on my listview, Typeface only works for textviews. Is there any way to set a custom font easily? Thank you.

Eulante
  • 309
  • 2
  • 4
  • 15
  • set the custom font to textviews in your listview – Mohammad Ersan Dec 24 '12 at 15:58
  • 2
    Use a custom list adapter and set the font in the "getView" override. http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ – Kuffs Dec 24 '12 at 16:00
  • @Kuffs so you said that when i override getView method i also insert a Typeface to change the font? – Eulante Dec 24 '12 at 17:23
  • @MoshErsan I can't, its the reason why I'm asking :D – Eulante Dec 24 '12 at 17:24
  • Here is an example of setting the font type on a text view: http://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-textview What can't you do - you don't know how, or there is something preventing the use of an adapter ? – samus Dec 24 '12 at 18:10
  • @SamusArin I know how change the font in a textview, i dont know how change it in a listview. Are they equal? – Eulante Dec 24 '12 at 18:25
  • 1
    @Eulante To clear things up, each line/entry in a ListView is enclosed in a TextView (so its a list of TextView's). So, your goal is grab each TextView and set the font style on it (I'm not aware of being able to set it "globally" for the entire ListView). You can do this in a ListViewAdapter. If you look at my answer below (the second one down), you'll see how this is done. If you need a snippet in Java code, let me know, but the translation is pretty straight forward. Understand now ? – samus Dec 24 '12 at 18:46
  • Good tutorial on ListView's (you don't need to use a ListActivity to use a ListView, its just a convenience thing): http://www.vogella.com/articles/AndroidListView/article.html – samus Dec 24 '12 at 18:48
  • 1
    Here, this is actually a duplicate question, check out the answer: http://stackoverflow.com/questions/7361135/how-to-change-color-and-font-on-listview – samus Dec 24 '12 at 18:52
  • I think i don't exactly understand your Question. Where do you want to apply your Font? If you use a custom ListAdapter it should be easy to apply the Font in your `public View getView(int position, View convertView, ViewGroup parent) {...}` method. –  Dec 24 '12 at 15:58
  • I have a listview in my activity and i want that each row has the same font which is not the deafult font but chosen by myself. – Eulante Dec 24 '12 at 17:21
  • @SamusArin Thank you, I've finally understood how listview works: its like an array of textviews, not? :) By the way, can you pm or email me the snippet you talked? Thank you again and Merry Christmas. – Eulante Dec 25 '12 at 10:08

1 Answers1

-1

You could create an Adapter for your list view, and set the font type on the TextView there.

Here is a clip of an Adapter (its in C#, but its not too different than Java):

    class ParcelRecordContactAdapter : ArrayAdapter<ParcelRecordContact>
    {
        List<ParcelRecordContact> contacts;

        public ParcelRecordContactAdapter(Context context, int textViewResourceId, List<ParcelRecordContact> contacts)
            : base(context, textViewResourceId, contacts)
        {
            this.contacts = contacts;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View v = convertView;

            if (v == null)
            {
                LayoutInflater li = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);

                v = li.Inflate(Resource.Layout.ListItem_SingleLine, null);
            }

            ParcelRecordContact contact = contacts[position];

            if (contact != null)
            {
                /**********************************************************
                 * change font on tv here
                 **********************************************************/

                TextView tv = (TextView)v.FindViewById(Resource.Id.tv_single_line_list_item_1);

                if (tv != null)
                {                        
                    tv.Text = "android is fu.... n"

                    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/DroidSansFallback.ttf");

                    tv.setTypeface(tf);
                }
            }

            return v;
        }
    }
samus
  • 6,102
  • 6
  • 31
  • 69
  • This question is actually a duplicate. Check out the answer here (in Java, haters): http://stackoverflow.com/questions/7361135/how-to-change-color-and-font-on-listview – samus Dec 24 '12 at 18:53