Android Listview Text Color

Android ListView Text Color

Ok, here are some things that you should be clear about:

  1. The background color you are setting in your xml file is of the activity and not of the ListItems you are trying to define.
  2. Every list item has its own layout file which should be passed or inflated in case you are using complex layout for list item.

I'll try to explain this with a code sample:

****Lets start with ListItems layout** : save it in your res/layout folder of you Android project with say **list_black_text.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Definig a container for you List Item-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- Defining where should text be placed. You set you text color here-->
<TextView
android:id="@+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:text="sample"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Well, a simple layout with a TextView to be precise. You must have an id assigned to TextView in order to use it.

Now coming to you screen/activity/chief layout, as I said you are defining background to your screen with android:background attribute. I see you have defined a TextView there as well and I suspect you are trying to define content/list item there, which is not at all needed.

Here's your edited layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">

<ListView
android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- REMOVED TEXT VIEW, AND KEEPING BACKGROUND WHITE -->
</LinearLayout>

And lastly, most importantly, set your adapter.

setListAdapter(new ArrayAdapter<String>(
this, R.layout.list_black_text, R.id.list_content, listItems));

Notice the layout resource which we are passing to adapter R.layout.list_black_text, and R.id.list_content which is TextView ID we declared. I have also changed ArrayAdapter to String type since it's generic.

I hope this explains everything. Mark my answer accepted if you agree.

Messy but a good quick fix way

You can also do this with a quick fix if you do not want to go ahead with complex layout defining etc.

While instantiating the adapter declare an inner class to do this, here is the code sample:

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

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

TextView textView=(TextView) view.findViewById(android.R.id.text1);

/*YOUR CHOICE OF COLOR*/
textView.setTextColor(Color.BLUE);

return view;
}
};

/*SET THE ADAPTER TO LISTVIEW*/
setListAdapter(adapter);

How to change the text color of a ListView item?

You will need to create a custom layout for your ListView items where you set your desired textcolor.

In this way, you do not even need a custom-adapter.

e.g. custom_textview.xml

<?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"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"/>

Then, you can use your layout with the ArrayAdapter:

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

Change textcolor programmatically in listview who depends of arrayadapter

try this:

ArrayList<String> arrayList = new ArrayList<>();
list = (ListView) findViewById(R.id.list1);

arrayAdapter = new ArrayAdapter<String>(INGR_NOTAS.this, R.layout.list_item_clickable, R.id.txtitem, arrayList){
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
((TextView)view.findViewById(R.id.txtitem)).setTextColor(position % 2 == 0 ? Color.WHITE : Color.RED); // here can be your logic
return view;
};
};

list.setAdapter(arrayAdapter);

hope it helps

Android change listview item text color

You can override the getView method of 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;
}
};

Can I change ListVIew Text Color?

Here, do these steps

  • Go to sdk folder \sdk\platforms\android-\data\res\layout
  • Copy simple_list_item_1 and paste it in your projects res\layout folder
  • Now open \res\layout\simple_list_item_1
  • Add color attribute there.
  • then change your

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

    to

    adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_list_item_1, country);

Android change TextView Font color in a ListView for a condition

Show Us db_msg this layout in that layout there is one TextView just get the name of that and replace with "tvIDFrom_db_msg_layout" this

adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view1 = super.getView(position, convertView, parent);
if (position % 2 == 0) { //Place the condition where you want to change the item color.
testo = messaggi.get(position);
TextView tvText = (TextView) view1.findViewById(R.id.tvIDFrom_db_msg_layout);
if(testo.substring(0,5).equals("27-09")){

tvText.setTextColor(Color.parseColor("#yourHexCode"));
} else {
//Setting to default color.
tvText.setTextColor(Color.WHITE);
}
return view1;
}
};

How to change color and font on ListView

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


Related Topics



Leave a reply



Submit