Vinoth
Vinoth

Reputation: 5785

Android listview focus

I am developing a playlist app for Android which has a ListView that contains song name (TextView) as its list item.

As the song plays, i want to highlight the item in the listview until the song ends. This is to indicate which song is currently playing.

I tried setSelection API from listview but it didn't work.

Upvotes: 1

Views: 525

Answers (1)

Beowulf Bjornson
Beowulf Bjornson

Reputation: 1626

Your list item view must implement the Checkable interface:

public class CheckableTextView extends TextView implements Checkable {

    private boolean mChecked = false;

    public CheckableTextView(Context context) {
        super(context);
    }

    public CheckableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {       
        mChecked = checked;
        setBackgroundDrawable(checked ? new ColorDrawable(getResources().getColor(android.R.color.white)) : null);
    }

    @Override
    public void toggle() {
        mChecked = !mChecked;
        setBackgroundDrawable(mChecked ? new ColorDrawable(getResources().getColor(android.R.color.white)) : null);
    }

}

Then you can use setItemChecked(int index, boolean checked) on your list.

Upvotes: 3

Related Questions