Search code examples
androidactionbarsherlock

Capturing optionsMenu spinner clicks


I am updating my api 8+ app for the reason that a large section of the target market no longer has a Menu button on their device.

My solution is not working.

So, I'm using ActionBarSherlock.

I've moved the buttons I needed to into the Sherlock menu, and they work ok.

But the activity I am changing had a Spinner which changed the data returned. I am trying to add the Spinner into the Sherlock menu (it does appear), but I can't capture any clicks on it.

Here is a screenshot. The top spinner is the new one; everything in the purple block is just there to compare.

enter image description here

This is the code I am using set the menu and capture the clicks:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    menu.add(0, 1, 1, "Add Sleep Record").setIcon(R.drawable.btn_add)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    menu.add(0, 2, 2, "Create Report").setIcon(R.drawable.ic_listview_pdf)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);


    Spinner spinner = new Spinner(getSherlockActivity().getSupportActionBar()
            .getThemedContext());
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getSherlockActivity()
            .getSupportActionBar().getThemedContext(), R.layout.sherlock_spinner_item,
            new String[]{"Last 7 days", "Last month", "Last 6 months", "Last year"});
    spinnerArrayAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    spinner.setAdapter(spinnerArrayAdapter);

    menu.add(0, 3, 3, "Date Range")
    .setActionView(spinner).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case 1:
        Intent i=new Intent(getActivity(), SleepRecordAddActivity.class);
        startActivity(i);
        return true;
    case 2:
        tryCreateReport();
        return true;
    case 3:
        String boop ="I am here";

        String x= boop;
        return true;
    }
    return false;

}

I've set the Spinner id to be 3, but the switch on onOptionsItemSelected doesn't ever trigger on 3.

This 'activity' is a SherlockFragment which extends from a SherlockFragmentActivity


Solution

  • Huh, turns out that it doesn't access onOptionsItemSelected at all. You just have to add an OnItemSelectedListener as usual.

    Spinner spinner = new Spinner(getSherlockActivity().getSupportActionBar()
                .getThemedContext());
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getSherlockActivity()
                .getSupportActionBar().getThemedContext(), R.layout.sherlock_spinner_dropdown_item,
                new String[]{"Last 7 days", "Last month", "Last 6 months", "Last year"});
        spinnerArrayAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
        spinner.setAdapter(spinnerArrayAdapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                        //do stuff
                    }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                //do stuff                
            }
    
        });
    
        menu.add(0, 3, 3, "Date Range")
        .setActionView(spinner).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);