Search code examples
androidbuttonselectionremoteview

Android. How do I keep a button displayed as PRESSED until the action created by that button is finished? Activated/Selected not usable


I want to keep a ImageButton with a state-list drawable in a "Wait-for-Action-Finished" state as long the action was not done.

I've seen the other topics around this problem. But in my case the other solutions are not working for me because:

  • I'm using the layout (which contains the buttons) in a RemoteView too (but not only there)
  • I'm using the same layout in a recyclerview with Multi-Selection capabilities. Therefore the activated/selected state is used by the multi-selector implementation and not possible for the state-list drawable except it was not propagated from selected-item to child views => my butotns
  • using custom state not working with RemoteViews
  • Using ToogleButton not possible, because setEnabled is not available for Buttons/ToggleButtons/CompoundButtons in RemoteViews

It's not important that the "Wait-for-Action-Finished" drawable was shown in the RemoteView (but it would be nice...), but I don't want to duplicate the layout. It's okay if the Wait-State was shown in the RecyclerView items only. But there it should work along with the Multiselector function.

I'm using an ImageButton because it can be disabled in a RemoteView (in opposite to Button/ToggleButton/CompoundButton in general).

any hints how I could solve this?


Solution

  • I've got it working by combining the selected and activated states.

    My button looks like this:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/progress_small"           android:state_activated="true" android:state_selected="true"/>
        <item android:drawable="@drawable/btn_bookmark_add_pressed" android:state_focused="true" android:state_pressed="true" />
        <item android:drawable="@drawable/btn_bookmark_add_pressed" android:state_focused="false" android:state_pressed="true" />
        <item android:drawable="@drawable/btn_bookmark_add_pressed" android:state_focused="true" />
        <item android:drawable="@drawable/btn_bookmark_add_normal"  android:state_focused="false" android:state_pressed="false" />
    </selector>
    

    And my button.onClicklistener looks like this:

    public void onClick(View v) {
        if (v.isActivated() && v.isSelected()) {
            return;
        }
        v.setActivated(true);
        v.setSelected(true);
        AppController.getJobManager().addJob(new Job()));
    }
    

    This way it won't collide neither with "click the itemView", where selected state will be propagated to all child views (inclusive my buttons) nor with activate the "multi-selection" feature where the activated state will be propagated to all child views.

    After the job is finished it will notify (EventBus) the RecyclerView where the ViewHolder can reset both activated and selected state back to false.