How to change text color of simple list item
Another simplest way is to create a layout file containing the textview you want with textSize, textStyle, color etc preferred by you and then use it with the ArrayAdapter.
e.g. mytextview.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/font_content"
android:padding="5sp"
android:layout_width="fill_parent"
android:background="@drawable/rectgrad"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"/>
and then use it with your ArrayAdapter as usual like
ListView lst = new ListView(context);
String[] arr = {"Item 1","Item 2"};
ArrayAdapter<String> ad = new ArrayAdapter<String>(context,R.layout.mytextview,arr);
lst.setAdapter(ad);
This way you won't need to create a custom adapter for it.
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);
Android ListView Text Color
Ok, here are some things that you should be clear about:
- The background color you are setting in your xml file is of the activity and not of the ListItems you are trying to define.
- 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);
Change text color of android.R.layout.simple_list_item_multiple_choice
Just make custom layout - simple_list_item_multiple_choice.xml
like this -
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
and use it in place of android.R.layout.simple_list_item_multiple_choice.xml
How to change text color of android.R.layout.simple_list_item_1?
android.R.layout.simple_list_item_1
contains only a TextView
. In this case you can cast the return value of super.getView(...)
avoiding the findViewById
:
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextColor(Color.BLACK);
return textView;
android.R.layout.simple_list_item_1 - TextColor is white - Android
It is so might be you are using a Dark theme
so the text is white
- Solution1
Change the theme to Theme.Light
<style name="LightThemeSelector" parent="android:Theme.Light">
...
</style>
- Solution2
Just add the textColor
to the textView
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#000000"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textAppearance="?android:attr/textAppearanceListItemSmall" />
Change ListView's textcolor
Check these for more info:
http://www.anddev.org/view-layout-resource-problems-f27/changing-listview-text-color-t14527.html
Changing text color of list view in android
Change Text Color in ListView
Make a layout for your List items and bind that to a ListAdapter.
Related Topics
How to to Check If a User Has Rated Your App on the Google Play
How to Check the Multiple Permission at Single Request in Android M
Simple Httpurlconnection Post File Multipart/Form-Data from Android to Google Blobstore
Android - Running a Method Periodically Using Postdelayed() Call
Android Gridview Row Dividers/Separators
Clear Recent Apps Wipe the Apps Memory and My Receiver Stopped Working
How to Monitor Sim State Change
How to Display More Than 3- Levels of Expandable List View
Set Width to Match Constraints in Constraintlayout
Android, How to Create Option Menu
How to Make Drawerlayout to Display Below the Toolbar
Adb Got Two Same Serial Numbers When Connected to Two Smart Phones
How to Send Data Message Using Firebase Console
What Is Setcontentview(R.Layout.Main)
Mediarecorder Start Failed: -38
Stuck with Getting Camera Pic When Using the Tab Activity
How to Record Screen and Take Screenshots, Using Android API