Question

I have a menu item on the action bar like

Arrays.xml has

<string-array name="days_of_the_week">
    <item>Sunday</item>
    <item>Monday</item>
    <item>Tuesday</item>
    <item>Wednesday</item>
    <item>Thursday</item>
    <item>Friday</item>
    <item>Saturday</item>
</string-array>

Activity:

public class ResultActivity extends SherlockListActivity implements ActionBar.OnNavigationListener{
//More code here
.
.

public void setupActionBar(){
        this.invalidateOptionsMenu();
        this.getSherlock().getActionBar().removeAllTabs();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(this, R.array.days_of_the_week, R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
        this.getSherlock().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        this.getSherlock().getActionBar().setListNavigationCallbacks(list, this);
}

I use ActionBarSherlock. By default the first element of the menu(Sunday) gets selected. But I want to give the current day as the default value.

Was it helpful?

Solution

Had I used a normal spinner, I could have set a default value by

String day = customCurrentDay();
spinner.setSelection(day);

Since I used ActionBarSherlock, ArrayAdapter and NavigationListener ,

I had to get the position of the default index that I want from the ArrayAdapter and setSelectedNavigationItem passing that as a parameter.

this.getSherlock().getActionBar().setListNavigationCallbacks(list, this);
this.getSherlock().getActionBar().setSelectedNavigationItem(list.getPosition(day));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top