Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

state_activated on pre Honeycomb Devices

You can not use the following state drawable as background for listview items.

<item android:drawable="@drawable/ic_launcher" android:state_activated="true"/>

On Pre Honeycomb Devices because this selector is not supported there and the android version does not keep track of the activated item(s).

How can this behaviour be emulated? Especially when using fragements (list on the left in one fragment and depending on what is selected a detail view on the right) this indicator is very important.

I know that this question was asked before here but the accepted answer there links to a blog article which states in "Step 4" that there is no possibility to have the activated indicator and instead only disables the use to prevent errors. This leads to the fact that no indicator is displayed which is what I'm searching for.

like image 675
theomega Avatar asked Nov 23 '12 14:11

theomega


2 Answers

I solved the problem using a small trick: By missusing the state_checked property, which exists since Android Version 1, it is possible to emulate the state_activated behaviour. There is no need to modify the List adapter or save the state yourself.

I wrote a detailed example containing all code necessary to repoduce and published it on a github repository.

like image 56
theomega Avatar answered Nov 09 '22 03:11

theomega


Much Like Chris Jenkins suggested, I just add a little code to my getView override:

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

    // ...

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        convertView.setBackgroundColor(Color.WHITE);
        if (listView.isItemChecked(position)) {
            convertView.setBackgroundColor(Color.LTGRAY);
        }
    }
}

The item being checked or not was automatically handled in my case (using CHOICE_MODE_MULTIPLE and v7 ActionMode.Callback with setOnItemLongClickListener calling the callback)

like image 33
Adam Johns Avatar answered Nov 09 '22 04:11

Adam Johns