How to change color and font on ListView
Asked Answered
J

9

21

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

Jordans answered 9/9, 2011 at 11:45 Comment(2)
Are you trying to set the color and size while list is created? Or do it to particular child after list is created?Laterality
I want to change the color and size before they are created. I int childCount = lv.getChildCount(); but get 0.how to fix itJordans
P
32

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);
Prismatoid answered 9/9, 2011 at 12:0 Comment(2)
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 ?Cherianne
If you add generics to this adapter, its much betterOutrun
L
7

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

Lietman answered 9/9, 2011 at 12:30 Comment(0)
I
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;
    }
Inorganic answered 21/6, 2013 at 1:4 Comment(0)
L
1

You can select a child like

TextView tv = (TextView)lv.getChildAt(0);
tv.setTextColor(Color.RED);
tv.setTextSize(12);    
Laterality answered 9/9, 2011 at 11:59 Comment(1)
tried this but getting null on tv = (TextView)lv.getChildAt(i);Jordans
F
1

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"
Foofaraw answered 9/9, 2011 at 13:30 Comment(0)
S
1

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;
    }


 }
Stationer answered 19/6, 2013 at 8:47 Comment(0)
C
1

If you want to use a color from colors.xml , experiment :

   public View getView(int position, View convertView, ViewGroup parent) {
        ... 
        View rowView = inflater.inflate(this.rowLayoutID, parent, false);
        rowView.setBackgroundColor(rowView.getResources().getColor(R.color.my_bg_color));
        TextView title = (TextView) rowView.findViewById(R.id.txtRowTitle);
        title.setTextColor(
            rowView.getResources().getColor(R.color.my_title_color));
        ...
     }

You can use too:

private static final int bgColor = 0xAAAAFFFF;
public View getView(int position, View convertView, ViewGroup parent) {
        ... 
        View rowView = inflater.inflate(this.rowLayoutID, parent, false);
            rowView.setBackgroundColor(bgColor);
...
}
Cognomen answered 7/3, 2015 at 14:33 Comment(0)
S
0

use them in Java code like this:

 color = getResources().getColor(R.color.mycolor);

The getResources() method returns the ResourceManager class for the current activity, and getColor() asks the manager to look up a color given a resource ID

Saunderson answered 9/9, 2011 at 12:6 Comment(0)
C
0

in android 6.0 you can change the colour of text like below

holder._linear_text_active_release_pass.setBackgroundColor(ContextCompat.getColor(context, R.color.green));
Cornish answered 7/12, 2016 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.