king_below_my_lord
king_below_my_lord

Reputation: 682

How to create a secondary drop down list in ActionBar?

I have the following code which already create a dropdown menu

public class MainActivity extends FragmentActivity implements
    ActionBar.OnNavigationListener {

private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar to show a dropdown list.
    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    actionBar.setDisplayShowHomeEnabled(false);

    // Set up the dropdown list navigation in the action bar.
    actionBar.setListNavigationCallbacks(
    // Specify a SpinnerAdapter to populate the dropdown list.
            new ArrayAdapter<String>(actionBar.getThemedContext(),
                    android.R.layout.simple_list_item_1,
                    android.R.id.text1, new String[] {
                            getString(R.string.demo1),
                            getString(R.string.demo2),
                            getString(R.string.demo3)}), this);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Restore the previously serialized current dropdown position.
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
        getActionBar().setSelectedNavigationItem(
                savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Serialize the current dropdown position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
            .getSelectedNavigationIndex());
}

@Override
public boolean onNavigationItemSelected(int position, long id) {
    // When the given dropdown item is selected, show its contents in the
    // container view.
    Fragment fragment = new MainFragmentSection();
    Bundle args = new Bundle();
    args.putInt(MainFragmentSection.ARG_SECTION_NUMBER, position + 1);
    fragment.setArguments(args);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).commit();
    return true;
}
public static class MainFragmentSection extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    public MainFragmentSection() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main_fragment,
                container, false);
        TextView dummyTextView = (TextView) rootView
                .findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return rootView;
    }
}
   }

what I want to do is add another dropdown list in the same action bar right next to the already existing dropdown menu in such a way that on user selection I could redraw the current fragment in Focus(which will be dependant on the first Spinner or drop down menu) with some small changes,like for sorting a string array in the current fragment view in focus.

Some guidance and assistance will be much appreciated.

Upvotes: 0

Views: 600

Answers (1)

MungoRae
MungoRae

Reputation: 1992

I would advise against this as the action bar is a standard design pattern in Android and customising it creates inconsistencies across apps which can confuse/frustrate users. However I understand sometimes you are forced to do this for various reasons.

So what you can do is create a custom view for your action bar. This allows you to design it mostly however you want. Here is a vogella tutorial that explains how:

http://www.vogella.com/articles/AndroidActionBar/article.html#menu_customviews

Basically you create a custom view in XML then add that to the action bar using either code or a theme style.

Hope that helps.

Upvotes: 1

Related Questions