3

I'm trying to make a contextual ActionBar. I used the sample from the android devpage, but i still haven't got it to work. I set an eventlistener on onitemlongclick, but the setSelected(true) doesn't seem to do anything. I know that the event is triggered, because the actionmode is opened, but it doensn't select any item.

The longclicklistener is in a fragment, held by a viewpager in an activity, which holds several instances of the fragment. I would like to be able to select items from the pages and then do something with the selections.

My current code:

The fragment:

AdapterView.OnItemLongClickListener onLongClick = new AdapterView.OnItemLongClickListener() {
    // Called when the user long-clicks on someView
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view,
            int i, long l) {
        MainActivity parent = (MainActivity)getActivity();
        if (parent.actionMode != null) {
            return false;
        }

        parent.actionMode = getActivity().startActionMode(parent.actionModeCallback);
        view.setSelected(true);
        return true;
    }
};

The action mode callback in the activity

public ActionMode.Callback actionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.contextual_actionbar, menu);
        return true;
    }

    // Called each time the action mode is shown. Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_remove_list:
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        actionMode = null;
    }
};
BC2
  • 892
  • 1
  • 7
  • 23
  • Do you set the OnLongClickListener to your view? Your code sample doesn't include the line someView.setOnLongClickListener(onLongClick)? Are you sure the onItemLongClick() is called at all? – Emanuel Moecklin Mar 03 '13 at 04:39
  • Oh, im sorry for being unclear. Yes the onlongclicj is called, the actionmode appears, but nothing gets selected. I have a diffrent style when it gets selected, so it's not only that the background doesn't change. – user1744968 Mar 03 '13 at 07:10
  • You might want to post the styles applied to the view then and the view xml (or code) – Emanuel Moecklin Mar 03 '13 at 14:46

1 Answers1

0

this is an old post, but for all newcomers...

For the view to change colors when selected is set you need to use a color state list. A color state list is an xml resource file that specifies which style should be applied depending on the state the view is in(Selected is one of those states). Something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:color="@color/textColor" android:drawable="@color/Background" android:state_selected="true"/>
<item android:color="@color/textColor" android:drawable="@color/Background"/>

</selector>

Save the XML file here: res/color/filename.xml.

You have to set your android:background(of whichever property u need to change depending on the state, i.e. android:textColor) on the view to this XML resource.

Then when you setSelected(true) the appropriate style is applied. Note that @drawable is used for background color while @color is used for other components such as text color.

Amin.MasterkinG
  • 805
  • 1
  • 12
  • 23
Osagui Aghedo
  • 330
  • 4
  • 12