-1

I'm a beginning Android developer and I want to learn how to implement dropdown navigation in my app. Essentially, I would like a different layout to show on the screen when the user selects an item in a spinner in the action bar.

I created a new activity with the Dropdown navigation template in Android Studio but I don't know how to proceed. How would I go about accomplishing this?

2 Answers2

0

To have a different view in the action bar spinner than in the spinner list, you can use a BaseAdapter or an ArrayAdapter and override some methods:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // Return a view which appears in the action bar.

    return yourCustomView..;
  }

  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // Return a view which appears in the spinner list.

    // Ignoring convertView to make things simpler, considering
    // we have different types of views. If the list is long, think twice!
    return super.getView(position, null, parent);
  }

More specific details can be found from here: https://stackoverflow.com/a/15293471/1650683

==== EDIT ====

You can find how to integrate dropdown in ActionBar in This article, follow the instruction, you will get it done.

As to bring to a different layout after selecting an item on the spinner, you should set fragment after user clicks on the dropdown item. Sample code:

mOnNavigationListener = new OnNavigationListener() {

  // Get the same strings provided for the drop-down's ArrayAdapter
  String[] strings = getResources().getStringArray(R.array.action_list);

  @Override
  public boolean onNavigationItemSelected(int position, long itemId) {
    // Create new fragment from our own Fragment class
    ListContentFragment newFragment = new ListContentFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    // Replace whatever is in the fragment container with this fragment
    // and give the fragment a tag name equal to the string at the position
    // selected
    ft.replace(R.id.fragment_container, newFragment, strings[position]);

    // Apply changes
    ft.commit();
    return true;
  }
};
Community
  • 1
  • 1
Wesley
  • 4,084
  • 6
  • 37
  • 60
  • Thanks, but my question is much simpler than that. All I want to figure out is how to set up dropdown navigation so that the user is brought to a different layout after selecting an item on the spinner. – INVV Rahul Jul 15 '14 at 03:26
  • @INVVRahul Edited the answer, hope it helps – Wesley Jul 15 '14 at 03:52
0

Use spinner for dropdown.This may help you.

 ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this, android.R.layout.simple_spinner_item, array_name); 
    adapter.setDropDownViewResource(R.layout.spinner_layout);
 final Spinner s1 = (Spinner) findViewById(R.id.spinner1);
 s1.setPrompt("Your Title");
 s1.setAdapter(adapter);
 s1.setOnItemSelectedListener(new OnItemSelectedListener()
    {   
       @Override
       public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3)
            {   
                 --enter your code here--
       }}
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {    
            }
    });  
ORIGINAL
  • 179
  • 1
  • 12