How to change font in a Listview?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to change font in a Listview?

Postby askdon2001 » Tue Jan 13, 2009 3:54 pm

Hi,

I am new to Android Development, and trying to develop a sample application. I have been trying to change the font within the listview, but I am unable to do it. I have tried googling the topic, but unable to find a solution.

Please see the attached image to see what I want to do. I want to have different fonts within the same Listview.

Best Regards,

Akash Dongre
Attachments
How to have different fonts in the listview.JPG
How to have different fonts, within the ListView like shown on the image.
How to have different fonts in the listview.JPG (9.31 KiB) Viewed 4163 times
Akash Dongre
askdon2001
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 12, 2008 3:12 pm

Top

Postby MrSnowflake » Tue Jan 13, 2009 3:57 pm

You can't change the font in a ListView as a ListView doesn't have a font. You need to change the font of the TextViews you use to display text in the ListView.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby askdon2001 » Tue Jan 13, 2009 4:00 pm

Thanks MrShowflake, but can I have different fonts? How do I change those?

I will try few things and will get back again.

Btw is their any link, where I can see all the attributes which can are used for Layouts. I really don't like the way Google has made the current API Doc.

Best Regards,

Akash Dongre
Akash Dongre
askdon2001
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 12, 2008 3:12 pm

Postby askdon2001 » Tue Jan 13, 2009 4:14 pm

Actually, I want to change the font of the text which is sent to a ArrayAdapter. The text is added to the arrayadapter using toString(), but how to have different fonts for the text which is added using toString().

Thanks in advance.

Best Regards,

Akash Dongre
Akash Dongre
askdon2001
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 12, 2008 3:12 pm

Postby MrSnowflake » Tue Jan 13, 2009 4:21 pm

The ArrayAdapter takes the resource Id of a layout which will be used to display the items in the array, so you have to make your own layout with the textviews you want (and of course your wanted font).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby askdon2001 » Tue Jan 13, 2009 4:54 pm

Thanks MrShowflake.

I was able to move forward with the issue. Now I have my layout, on which I am working. Still I and to know how to have different fonts within the last Arrayadapter, were the text is inserted using toString();

Thanks in advance again.

Best Regards,

Akash Dongre
Akash Dongre
askdon2001
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 12, 2008 3:12 pm

Top

Postby MrSnowflake » Tue Jan 13, 2009 5:27 pm

Show me your code.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby askdon2001 » Tue Jan 13, 2009 5:46 pm

Display:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void onCreate(Bundle savedInstanceState) {
  3.  
  4.                 // TODO Auto-generated method stub
  5.  
  6.                 requestWindowFeature(getWindow().FEATURE_NO_TITLE);
  7.  
  8.                 super.onCreate(savedInstanceState);
  9.  
  10.                 setTheme(0x1020022);
  11.  
  12.  
  13.  
  14.                 getWindow().setBackgroundDrawableResource(R.drawable.xyz);
  15.  
  16.  
  17.  
  18.                 setContentView(R.layout.show);
  19.  
  20.  
  21.  
  22.                 TextView feedname = (TextView) findViewById(R.id.feedname);
  23.  
  24.                 itemlist = (ListView) findViewById(R.id.itemlist);
  25.  
  26.  
  27.  
  28.                 if (XYZ.feed == null) {
  29.  
  30.                         feedname.setText("Sorry, No search results found");
  31.  
  32.                         return;
  33.  
  34.                 } else if (XYZ.feed.getItemCount() == 0) {
  35.  
  36.                         feedname.setText("Sorry, No search results found");
  37.  
  38.                         return;
  39.  
  40.                 } else {
  41.  
  42.  
  43.  
  44.                        
  45.  
  46.                         ArrayAdapter<FeedItem> adapter = new ArrayAdapter<FeedItem>(this,
  47.  
  48.                                         R.layout.itemlist, XYZ.feed
  49.  
  50.                                                         .getAllItems());
  51.  
  52.  
  53.  
  54.                         itemlist.setAdapter(adapter);
  55.  
  56.                         itemlist.setOnItemClickListener(this);
  57.  
  58.                         itemlist.setSelection(0);
  59.  
  60.                        
  61.  
  62.  
  63.  
  64.                 }
  65.  
  66.         }
  67.  
  68.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


FeedItem:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class FeedItem
  3.  
  4. {
  5.  
  6.         private String _name = null;
  7.  
  8.         private String _address = null;
  9.  
  10.        
  11.  
  12.  
  13.  
  14.        
  15.  
  16.         FeedItem()
  17.  
  18.         {
  19.  
  20.         }
  21.  
  22.         void setname(String name)
  23.  
  24.         {
  25.  
  26.                 _name = name;
  27.  
  28.         }
  29.  
  30.  
  31.  
  32.         void setaddress(String address)
  33.  
  34.         {
  35.  
  36.                 _address = address;
  37.  
  38.         }
  39.  
  40.        
  41.  
  42.         String getname()
  43.  
  44.         {
  45.  
  46.                 return _name;
  47.  
  48.         }
  49.  
  50.        
  51.  
  52.         String getaddress()
  53.  
  54.         {
  55.  
  56.                 return _address;
  57.  
  58.         }
  59.  
  60.        
  61.  
  62.         }
  63.  
  64.         public String toString()
  65.  
  66.         {
  67.  
  68.        
  69.  
  70.                 String xyz =_name+"\nAddress: "+_address;
  71.  
  72.                 return xyz;
  73.  
  74.         }
  75.  
  76. }
  77.  
  78.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Feed:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Feed
  3.  
  4. {
  5.  
  6.         private String _name = null;
  7.  
  8.         private String _address = null;
  9.  
  10.        
  11.  
  12.         private int _itemcount = 0;
  13.  
  14.         private List<FeedItem> _itemlist;
  15.  
  16.        
  17.  
  18.        
  19.  
  20.         Feed()
  21.  
  22.         {
  23.  
  24.                 _itemlist = new Vector(0);
  25.  
  26.         }
  27.  
  28.         int addItem(FeedItem item)
  29.  
  30.         {
  31.  
  32.                 _itemlist.add(item);
  33.  
  34.                 _itemcount++;
  35.  
  36.                 return _itemcount;
  37.  
  38.         }
  39.  
  40.         FeedItem getItem(int location)
  41.  
  42.         {
  43.  
  44.                 return _itemlist.get(location);
  45.  
  46.         }
  47.  
  48.         List getAllItems()
  49.  
  50.         {
  51.  
  52.                 return _itemlist;
  53.  
  54.         }
  55.  
  56.         int getItemCount()
  57.  
  58.         {
  59.  
  60.                 return _itemcount;
  61.  
  62.         }
  63.  
  64.         void setname(String name)
  65.  
  66.         {
  67.  
  68.                 _name = name;
  69.  
  70.         }
  71.  
  72.         void setaddress(String address)
  73.  
  74.         {
  75.  
  76.                 _address = address;
  77.  
  78.         }
  79.  
  80.        
  81.  
  82.         String getname()
  83.  
  84.         {
  85.  
  86.                 return _name;
  87.  
  88.         }
  89.  
  90.        
  91.  
  92.        
  93.  
  94.         String getaddress()
  95.  
  96.         {
  97.  
  98.                 return _address;
  99.  
  100.         }
  101.  
  102.        
  103.  
  104.        
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

XML:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <!--
  5.  
  6. -->
  7.  
  8.  
  9.  
  10. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  11.  
  12.    android:id="@android:id/text1"
  13.  
  14.    android:layout_width="fill_parent"
  15.  
  16.    android:layout_height="wrap_content"
  17.  
  18.    android:textAppearance="?android:attr/textAppearanceMedium"
  19.  
  20.    android:gravity="center_vertical"
  21.  
  22.    android:minHeight="?android:attr/listPreferredItemHeight"
  23.  
  24.    
  25.  
  26. />
  27.  
  28.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


I want to have two different font sizes for Name and the address, like the attached image.

Thanks in advance.

Best Regards,

Akash Dongre
Akash Dongre
askdon2001
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 12, 2008 3:12 pm

Postby MrSnowflake » Tue Jan 13, 2009 6:09 pm

askdon2001 wrote:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. -->
  4.  
  5. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  6.    android:id="@android:id/text1"
  7.    android:layout_width="fill_parent"
  8.    android:layout_height="wrap_content"
  9.    android:textAppearance="?android:attr/textAppearanceMedium"
  10.    android:gravity="center_vertical"
  11.    android:minHeight="?android:attr/listPreferredItemHeight"
  12.    
  13. />
  14.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


I want to have two different font sizes for Name and the address, like the attached image.
Then you have to make a layout which has 2 TextViews (@android:id/text1 and @android:id/text2) with 2 different fonts... (Don't forget to put them both in a ViewGroup).

And you should really use proper capitalization, instead of: String getaddress() do String getAddress(). It makes your life a lot easier. You probably knew this already, as you did some of them correctly :).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby askdon2001 » Wed Jan 14, 2009 6:15 pm

Thanks Snowflake.

But unfortunately, I am still stuck. I don't know how to put TextView in a viewgroup. Also, what will go into arrayadaptor?

I have tried making two list and tried to put them, but go error.

Can you please let me know about some example of putting TextView in ViewGroup.

Thanks and Regards,

Akash Dongre
Akash Dongre
askdon2001
Freshman
Freshman
 
Posts: 6
Joined: Wed Nov 12, 2008 3:12 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests