User7723337
User7723337

Reputation: 12018

How to change text color of simple list item

I have an ListActivity and i am displaying one list with:

setListAdapter(new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_single_choice, mStringList));

By default text color of list items is white, I want to change text color of text views in the list to black.

How should i do it?

Upvotes: 44

Views: 111961

Answers (12)

mattfred
mattfred

Reputation: 2749

The simplest way to do this without needing to create anything extra would be to just modify the simple list TextView:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, strings) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView = (TextView) super.getView(position, convertView, parent);
                textView.setTextColor({YourColorHere});
                return textView;
            }
        };

Upvotes: 8

Beto Caldas
Beto Caldas

Reputation: 2323

If you are creating a class that extends an Adapter, you can use parent variable to obtain the context.

public class MyAdapter extends ArrayAdapter<String> {
private Context context;
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       context = parent.getContext();
       context.getResources().getColor(R.color.red);
       return convertView;
   }
}

You can do the same with RecyclerView.Adapter, but instead of getview() you will use onCreateViewHolder().

Upvotes: 1

Bruno Peres
Bruno Peres

Reputation: 3256

If you want to keep all the style but change few details, you can use the default style defined on the Android and change what you want

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:textColor="@android:color/background_light"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

Then set the adapter using:

setListAdapter(new ArrayAdapter<String>(getApplicationContext(),
            R.layout.list_item_custom, mStringList));

Source: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/layout/simple_list_item_activated_1.xml

Upvotes: 1

I'm also faced this situation. You can handle above method. but simple if you pass simple_dropdown_item_1line as resource. It will be black color text by default.

This is my code I have achieved in kind of situation.

ArrayAdapter<String> arrAdapter = 
new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line,countryList);
        listView.setAdapter(arrAdapter);

Your code below:

setListAdapter(new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_dropdown_item_1line, mStringList));

Actually android.R.layout.simple_dropdown_item_1line is used for dropdown. I urge u to see this layout content once. It just simple textview. thats all. It's property will not affect your task. I checked It works fine.

Upvotes: 2

kannappan
kannappan

Reputation: 2250

try this code...

<RelativeLayout 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:background="#ffff00"> 

    <ListView 
            android:id="@+id/android:list" 
            android:layout_marginTop="2px" 
            android:layout_marginLeft="2px" 
            android:layout_marginRight="2px"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:background="@drawable/shape_1" 
            android:listSelector="@drawable/shape_3" 
            android:textColor="#ffff00" 
            android:layout_marginBottom="44px" /> 
</RelativeLayout>

Upvotes: -1

user2797839
user2797839

Reputation: 11

Try this code in stead of android.r.layout.simple_list_item_single_choice create your own layout:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:gravity="center_vertical"
    android:padding="5dip"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/black"
    android:textStyle="bold">
</CheckedTextView>

Upvotes: 1

chip
chip

Reputation: 1789

You just have override the getView method of ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), 
        android.R.layout.simple_list_item_1, mStringList) {
    @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);
        text.setTextColor(Color.BLACK);
        return view;
    }
};

Upvotes: 39

Anu
Anu

Reputation: 551

Create an xml file in res/values and copy the below code

<style name="BlackText">
<item name="android:textColor">#000000</item>
</style>

and the specify the style in activity in Manifest like below

 android:theme="@style/BlackText"

Upvotes: 0

Cam Saul
Cam Saul

Reputation: 1737

I realize this question is a bit old but here's a really simple solution that was missing. You don't need to create a custom ListView or even a custom layout.

Just create an anonymous subclass of ArrayAdapter and override getView(). Let super.getView() handle all the heavy lifting. Since simple_list_item_1 is just a text view you can customize it (e.g. set textColor) and then return it.

Here's an example from one of my apps. I'm displaying a list of recent locations and I want all occurrences of "Current Location" to be blue and the rest white.

ListView listView = (ListView) this.findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations()) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) super.getView(position, convertView, parent);

        String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
        int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
        textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));

        return textView;
    }
});

Upvotes: 52

dharmin007
dharmin007

Reputation: 1612

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.

Upvotes: 91

Ajay Singh
Ajay Singh

Reputation: 1621

you can use setTextColor(int) method or add style to change text color.

<style name="ReviewScreenKbbViewMoreStyle">
<item name="android:textColor">#2F2E86</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">10dip</item>

Upvotes: 2

Ayudh
Ayudh

Reputation: 710

In simple word "you can't do it through simple setListAdapter" . you must used custom listview for freely changes in text color or in any other views

for Custom Listview you can go with this link

Upvotes: 4

Related Questions