user802023
user802023

Reputation: 201

Can I make one ListView item have a different Text Color?

I have the 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"
    >
<ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.0"
    android:textColor="@android:color/white"
    android:dividerHeight="1px"
    android:listSelector="@drawable/highlight_sel"
    />
</LinearLayout>

And the code:

private ListView lv1;
private String lv_arr[]={"Item 1","Item 2","Item 3","Item 4"};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newsmenu);

    lv1=(ListView)findViewById(R.id.ListView01);
    // By using setAdpater method in listview we an add string array in list.
    lv1.setAdapter(
       new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1,
                                lv_arr));
}

I want the text color of Item 2 (or 1 or 3 or 4) to appear dynamically as red (denoting a new item) or white (default). Is there a way to do this?

I already have a selector present, which is why I used ListView. I've search the Internet and this site, and I have not seen this question broached.

So is it possible?

Upvotes: 1

Views: 4971

Answers (3)

John Feagans
John Feagans

Reputation: 409

Writing a special adapter to override getView in simple adapter is the way to change the text color alternating on the lines of your choice in a listview. I took the example which has been repeated many times on this website and added a way to change the text color. position mod length to select the color position can be replaced with any scheme you like. The text view "business" can be the first line of your layout like mine--or use the android.R.id.text1.

public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };

public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
    super(context, items, resource, from, to);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = super.getView(position, convertView, parent);
  int colorPos = position % colors.length;
  //view.setBackgroundColor(colors[colorPos]); //old example
  TextView tv1 = (TextView)view.findViewById(R.id.business); //new
  tv1.setTextColor(colors[colorPos]); //new
  return view;
}
}

Just use SpecialAdapter instead of SimpleAdapter in your app.

Upvotes: 2

Caspar Harmer
Caspar Harmer

Reputation: 8117

Here's an example of a getView method. Note that it's using a viewholder for efficiency. If you want to know more about that, let me know.

public View getView(int position, View convertView, ViewGroup parent) {

    tempDeal = exampleBoxArrayList.get(position);

    ViewHolder holder;

    if (convertView == null) {
        convertView = inflator.inflate(R.layout.list_item_example_box, null);
        holder = new ViewHolder();
        holder.divider = (RelativeLayout) convertView.findViewById(R.id.example_box_divider);
        holder.merchantName = (TextView) convertView.findViewById(R.id.example_box_merchant_name);
        holder.expireDate = (TextView) convertView.findViewById(R.id.example_box_expire_date);
        holder.description = (TextView) convertView.findViewById(R.id.example_box_description);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();

    }

    if (tempDeal.isDivider()) {
        holder.divider.setVisibility(View.VISIBLE);
    } else {
        holder.divider.setVisibility(View.GONE);
    }

    holder.merchantName.setText(tempDeal.getMerchantName());
    holder.expireDate.setText(tempDeal.getExpiryDateString());
    holder.description.setText(tempDeal.getPriceOption().getDescription());

    return convertView;

}

As you can see, I call the isDivider() method on my custom object (this method looks at a boolean set on data load). This method is used to turn the visibility of part of the layout on or off.

Alternatively, you could load a completely new layout based on this same concept.

Upvotes: 0

DArkO
DArkO

Reputation: 16110

Yes everything is possible. you need to write your own adapter implementation basically overriding the getView Method in the adapter. search google and stack you will find many tutorials on how to write an adapter.

Upvotes: 3

Related Questions