The color of the text of the Android change list item.

I am trying to change the text color of some elements (or backgroung color) as a list based on a flag. after a long search, I did not find out how to do this, I call the lower loop after a certain action to change the color:

ListView _listView = (ListView) findViewById(R.id.listView2); for (int i=0;i<=_listView.getCount();i++) { if(Falg == True) { //here i want to change the list view item color or backgroud color } } 
+6
source share
4 answers

You can override the getView method of the Array adapter and change the color:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, myList) { @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView text = (TextView) view.findViewById(android.R.id.text1); if (flag == True) { text.setTextColor(Color.BLACK); } return view; } }; 
+26
source

You can do this directly in your custom Adapter .

See Adapter.getView ()

You can inflate a line in this method and dynamically change presentation colors and other things.

+3
source

Create a custom layout, define your color in this layout.

Like this:

 <? xml version="1.0" encoding=" utf-8 "? > <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv" android:textColor="@color/white" // Here You define your desired color android:layout_width="fill_parent" android:gravity="center" android:layout_height="fill_parent"/> 

Use this layout in your adapter, for example:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.custom_textview, info); lv.setAdapter(adapter); 

This way you do not need to use a custom adapter.

+3
source

You can set the font style of the font .... for example

 textView.setText(Html.fromHtml("<b><u>"+string+"<b><u>")); 

when you fill in the text on your list.

-4
source

All Articles