Joseph Rand
Joseph Rand

Reputation: 51

highlight single item listview

I am facing some problem using a listview to highlight a single item. I've created an anonymous adapter as in the code below but when the list is displayed several items have the highlight/bold. The log trace shows the bold/background only being called once and yet several items have the bold but only the correct one has the background color set. I separated the background color setting for debugging but the results are the same if I use only one if(position==selection) check.

Note that this is not an interactive listview, no onclicklistener needed.

I would greatly appreciate any help. Thank you in advance.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, times) {
    @Override
    public View getView(int position, View cview, ViewGroup group) {
        int count = 0; //used for debugging
        View view = super.getView(position, cview, group);
        TextView tv = (TextView)view.findViewById(android.R.id.text1);
        Typeface tf = tv.getTypeface();
        Log.d("log", "position = " + position);
        if(position == selection) {
            tv.setTypeface(tf, Typeface.BOLD);
            Log.d("log", "BOLD" + position + "count=" + (count ++));
        } else {
            tv.setTypeface(tf, Typeface.NORMAL);
        }
        if(position == selection) {
            view.setBackgroundColor(Color.LTGRAY);
            Log.d("log", "Bkgnd " + position + "count: " + count);
        } else {
            view.setBackgroundColor(Color.WHITE);
        }
        return view;
    }
};

Upvotes: 1

Views: 54

Answers (2)

Joseph Rand
Joseph Rand

Reputation: 51

I just wanted to add to this in case someone checks it. A good solution is as follows:

 public View (int position, View convertView, ViewGroup parent) {
   if(convertView == null) {
    convertView = LayoutInflater.from(context).inflate(R.layout.mylayout, parent, false);
   }
   TextView textView = convertView.findViewById(R.id.mytextview);
   if(position == selection) {
   textView.setBackgroundColor(Color.parseColor("#88FFFFFF");
   } else {
     textView.setBackgroundColor(Color.parseColor("#00000000");
   }
   return convertView;
 }

What this does is set the background color to white with 50% alpha for the selection and 0% alpha, transparent, for the non-selection. This makes the selected item a little brighter but the same color.

A similar effect can be found by setting android:listSelector to "#88FFFFFF" in xml but doing it in the getView() function allows one to highlight more than one element differently while keeping the same color theme. I used this in my program to highlight some items in a listview with "#88000000" a darker item plus the "#88FFFFFF" for the selected item.

anyway, not sure if anyone will come across this but I found it helpful once I figured it out

Upvotes: 1

Anhayt Ananun
Anhayt Ananun

Reputation: 896

Apparently, there is some "magic" with typefaces. I've changed the part that is responsible for setting the typeface as follows:

if(position == selection) {
    tv.setTypeface(Typeface.create(tf, Typeface.BOLD));
}
else {
    tv.setTypeface(Typeface.create(tf, Typeface.NORMAL));
}

and it works as expected.

Upvotes: 0

Related Questions