Change font size in ListView - Android/Eclipse

81,630

Solution 1

2 ways to go:

  1. Copy simple_list_item_1.xml from Android sources, modify it and then use it instead of android.R.layout.simple_list_item_1;
  2. Use BaseAdapter and modify font size in getView(..) call.

I'd suggest you go with latter.

Solution 2

Go into layout create a new xml file named mylist.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@android:id/text1"  
        android:paddingTop="2dip" 
        android:paddingBottom="3dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

now in the code

        adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);

change it to

        adapter = new ArrayAdapter<String> (this,R.layout.mylist,HistoryList);

Solution 3

Please create another file named list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:padding="6dp"
    android:textSize="21sp">

Share:
81,630
Soren
Author by

Soren

Updated on October 16, 2020

Comments

  • Soren
    Soren over 3 years

    How can I change the font size in a ListView element? In my main.xml file, I have tried several different values in for android:textSize (pt,px,sp,dp) and nothing seems to change it.

    Here is what I have currently for the in my main.xml:

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:textColor="#ffffff"
        android:background="#000080" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:clickable="true" 
        android:dividerHeight="1px"
        android:layout_marginTop="5px" 
        android:textSize="8px"/>
    

    Here is my Java:

    package com.SorenWinslow.TriumphHistory;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    
    public class TriumphHistory extends ListActivity {
        String[] HistoryList;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ArrayAdapter<String> adapter;
            HistoryList = getResources().getStringArray(R.array.history);
            adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
            setListAdapter(adapter);
        }
    
    }
    
  • Shouvik
    Shouvik almost 14 years
    How do i define where to change the size in get view?
  • tamsler
    tamsler over 12 years
    As Alex has suggested, it makes sense to override the Adapter's getView() method. I use this approach and it works well.
  • Garima Tiwari
    Garima Tiwari almost 11 years
    This solution does not work when I am trying to make a radio list out of the same, any ideas about that?
  • Shereef Marzouk
    Shereef Marzouk almost 11 years
    @GarimaTiwari What seems to be the problem ? what is the unexpected behavior you get when you try to do it ?
  • Naeem A. Malik
    Naeem A. Malik over 10 years
    I used option 1 since all I don't need to do much, over riding getView method sounds like an overkill IMO
  • JackLThornton
    JackLThornton about 7 years
    This is an incomplete answer at best. There is no mention of the method to call (setTextSize); the View object in the adapter is a ListView object, not the TextView associated with it; therefore 'setTextSize' is not a method against that view; if you figure that out and call 'findViewById' against the view you have to get the associated TextView, it shows an error when you try to use "android.R.layout.simple_list_item_1' as the id; etc. etc. Not sure why this answer was selected or upvoted as it would require a dozen follow-on questions to actually get something to work.