How to change color and font on ListView

109,565

Solution 1

You need to create a CustomListAdapter.

public class CustomListAdapter extends ArrayAdapter <String> {

    private Context mContext;
    private int id;
    private List <String>items ;

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list ) 
    {
        super(context, textViewResourceId, list);           
        mContext = context;
        id = textViewResourceId;
        items = list ;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent)
    {
        View mView = v ;
        if(mView == null){
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.textView);

        if(items.get(position) != null )
        {
            text.setTextColor(Color.WHITE);
            text.setText(items.get(position));
            text.setBackgroundColor(Color.RED); 
            int color = Color.argb( 200, 255, 64, 64 );
                text.setBackgroundColor( color );

        }

        return mView;
    }

}

The list item looks like this (custom_list.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

Use the TextView api's to decorate your text to your liking

and you will be using it like this

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);

Solution 2

Create a CustomAdapter and in that you have the getView() so there if you want to change the listview background color use this :

v.setBackgroundColor(Color.CYAN);

If you want to change the textColor then do this :

tv.setTextColor(Color.RED);

and for the textSize :

tv.setTextSize(20);

where 'v' is the listview and 'tv' is the textview

Solution 3

Even better, you do not need to create separate android xml layout for list cell view. You can just use "android.R.layout.simple_list_item_1" if the list only contains textview.

private class ExampleAdapter extends ArrayAdapter<String>{

    public ExampleAdapter(Context context, int textViewResourceId, String[] objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        View view =  super.getView(position, convertView, parent);

        TextView tv = (TextView) view.findViewById(android.R.id.text1);
        tv.setTextColor(0);

        return view;
    }

Solution 4

If u want to set background of the list then place the image before the < Textview>

< ImageView
android:background="@drawable/image_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

and if u want to change color then put color code on above textbox like this

 android:textColor="#ffffff"

Solution 5

If you just need to change some parameters of the View and the default behavior of ArrayAdapter its OK for you:

 import android.content.Context;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;

 public class CustomArrayAdapter<T> extends ArrayAdapter<T> {

    public CustomArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

            // Here all your customization on the View
            view.setBackgroundColor(.......);
            ...

        return view;
    }


 }
Share:
109,565
Vitaly Menchikovsky
Author by

Vitaly Menchikovsky

By day: worked at Amdocs for 2 years as FE &amp; BE Dev(JAVA,Backbone...), Now working at Gala coral as a FE dev with Backbone. Know also JS,Angular,redux,react, ionic, Java, Bootstrup,auto testing cucumber,firebase. Hobbies: know LR, photo Shop, and professional photography.

Updated on December 08, 2020

Comments

  • Vitaly Menchikovsky
    Vitaly Menchikovsky over 3 years

    I am trying to change my font(color and size) and the back ground on my ListView. I want to change it with code lines not on xml. my list view looks like: the 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="10dp"
    android:textSize="18sp" android:text="@string/hello">
    </TextView>
    

    and my code is

    public class NewsActivity  extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
    
     // ArrayAdapter listItemAdapter = new ArrayAdapter( this,android.R.layout.simple_list_item_1, v_itemList );
    
          setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,ynetList));
    
          View v=getListView() ;
    
          ListView lv = getListView();
    

    what next? please give me an example base on my code

  • Vitaly Menchikovsky
    Vitaly Menchikovsky over 12 years
    tried this but getting null on tv = (TextView)lv.getChildAt(i);
  • Pratik
    Pratik over 10 years
    I tried the code you have given here... Its working nice... one thing I want to customize is if position equals 0 then I need to change color for only that textview. it worked but If i scroll the suggestions, other textview color is also changed. What can I do to set only color of textview which is at position 1 ?
  • Shervin Asgari
    Shervin Asgari over 10 years
    If you add generics to this adapter, its much better