Android - how to change the typeface of all other rows of a list every time a row is selected?
Question
I set the typeface of a row of my list to bold when I click on it. I use the following code to do that -
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
View rowView = view;
TextView tv = (TextView) rowView.findViewById(R.id.label);
tv.setTypeface(null, Typeface.BOLD);
}
});
However, if I select another row, the previously selected row still stays bold. I want the typeface of that row to be changed to normal. How could I achieve that?
Solution
Keep the position passed as an argument to onItemClick in a member variable of your class. Then on each onItemClick if this field is initialized with a proper value( i.e != -1) do like this
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(mPreviouslySelectedPosition != INVALID_POSITION)
{
final View rowView = parent.getItemAtPosition(mPreviouslySelectedPosition);
((TextView)rowView.findViewById(R.id.label1)).setTypeface(null,Typeface.NORMAL);
}
TextView tv = (TextView) view.findViewById(R.id.label);
tv.setTypeface(null, Typeface.BOLD);
mPreviouslySelectedPosition = position;
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow