GMX
GMX

Reputation: 960

Change color of one line of a ListView

I want make a ListView that have some elements with the WHITE text color and some other elements with the BLACK text color.

Here is my code for now:

ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(My_Activity.this, R.layout.row_lay, R.id.textViewList, myarray);
listLead.setAdapter(arrayAdapter);

I can modify the TextColor of my R.layout.row_lay but this change the color for all the Line of my ListView. How can I do for example one line a color and the other another one? Is possible to achieve that ? Different TextColor in the same ListView?

If yes how?

Upvotes: 0

Views: 1211

Answers (2)

Pztar
Pztar

Reputation: 4749

You're going to have override the getView method of the ArrayAdapter

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(My_Activity.this, R.layout.row_lay, R.id.textViewList, myarray) { 

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

    //your condition logic
    v.setBackgroundColor(yourColor);

    return v;
    }
};

Upvotes: 2

LukeWaggoner
LukeWaggoner

Reputation: 8909

Create a custom Adapter class that extends ArrayAdapter, and override getView like so:

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

    TextView text = (TextView) v.findViewById(R.id.my_textview_id);
    if (position == whiteColoredPosition){
        text.setTextColor(Color.WHITE);
    } else {
        text.setTextColor(Color.BLACK);
    }

    return v;
}

Where R.id.my_textview_id is the ID of your TextView and whiteColoredPosition is the position in the ListView containing the TextView you want to be white.

Upvotes: 1

Related Questions