Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop Down Menu on Action bar

I am working on an android application Menu with Action Bar, I want to put the dropdown menu in the action bar like the one present in Google Maps application. Google Maps ActionBar

Can somebody help me? How to achieve this Please point to some easy tutorial that I can follow.

like image 774
Rookie Avatar asked Jul 07 '12 15:07

Rookie


People also ask

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Where is the action bar on my phone?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What is the difference between a toolbar and an action bar?

An Action bar is traditionally a part of an Activity opaque window decor controlled by the framework but a Toolbar may be placed at any level of nesting within a view hierarchy. The toolbar provides more feature than ActionBar . A Toolbar may contain a combination of elements from start to end.


2 Answers

Add this to your activity onCreate() method:

// Adapter
SpinnerAdapter adapter =
        ArrayAdapter.createFromResource(this, R.array.actions,
        android.R.layout.simple_spinner_dropdown_item);

// Callback
OnNavigationListener callback = new OnNavigationListener() {

    String[] items = getResources().getStringArray(R.array.actions); // List items from res

    @Override
    public boolean onNavigationItemSelected(int position, long id) {

        // Do stuff when navigation item is selected

        Log.d("NavigationItemSelected", items[position]); // Debug

        return true;

    }

};

// Action Bar
ActionBar actions = getActionBar();
actions.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actions.setDisplayShowTitleEnabled(false);
actions.setListNavigationCallbacks(adapter, callback);

This example requires an array resource for the list items:

res/values/arrays.xml

<string-array name="actions">
    <item>Item 1</item>
    <item>Item 2</item>
    <item>Item 3</item>
</string-array>

Alternatively you could create your own adapter and layout extended from SpinnerAdapter to display more advanced or dynamic list items.

To make the activity onCreate code even neater you could also change your Activity to implement OnNavigationListener and add the override onNavigationItemSelected with the callback code. Then change "callback" to "this" in the setListNavigationCallbacks() method.

Please note you will need to target API 11+ for the action bar, otherwise you will need to add version checking or a support library.

like image 128
tpbapp Avatar answered Oct 03 '22 21:10

tpbapp


check this Link also it is helpful example Example link

GoogleMap map;

TextView txt;

String[] mapTypes={"Normal","Hybrid","Satellite","Terrain"};

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

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    txt=(TextView)findViewById(R.id.textView1);

//to set map Type
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

//To set the marker on map on specific location using lat lag
    // latitude and longitude
    double latitude = 18.520430300000000000;
    double longitude = 73.856743699999920000;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Location");

    // change color to the marker icon
    marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));

    // adding marker
    map.addMarker(marker);

    // Create an array adapter to populate dropdownlist 
    ArrayAdapter<String> adapter =new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, mapTypes);

    /** Enabling dropdown list navigation for the action bar */
    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    /** Defining Navigation listener */
    ActionBar.OnNavigationListener navigationListener=new ActionBar.OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            Toast.makeText(getBaseContext(), "U Select : "+mapTypes[itemPosition], Toast.LENGTH_SHORT).show();
            if (mapTypes[itemPosition].equals("Normal")) {
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            }
            else if (mapTypes[itemPosition].equals("Hybrid")) {
                map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            }
            else if (mapTypes[itemPosition].equals("Satellite")) {
                map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            }
            else if (mapTypes[itemPosition].equals("Terrain")) {
                map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            }
            return false;
        }
    };

    // Setting dropdown items and item navigation listener for the actionbar 
    getActionBar().setListNavigationCallbacks(adapter, navigationListener);

}
like image 1
SAndroidD Avatar answered Oct 03 '22 20:10

SAndroidD