Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color and font on ListView

Tags:

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

like image 433
Vitaly Menchikovsky Avatar asked Sep 09 '11 11:09

Vitaly Menchikovsky


People also ask

How do I edit text in ListView?

Configure the edittextcreate an adapter for the listview and set the position as a tag for the edittext. Normally, when scrolling the item position will change. So, you have to get the edittext tag and set it into the edittext id. from that you can avoid the change of the item position.

How do I change the text color on my adapter?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.


2 Answers

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); 
like image 98
Reno Avatar answered Oct 02 '22 02:10

Reno


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

like image 22
Lavanya Avatar answered Oct 02 '22 03:10

Lavanya