Search code examples
androidmenu-items

How to creat a menu with icons?


I really need some guide or suggestion about how to creat a menu with icons.

I am write an Android app, and I want to creat a menu like this, when I enter this app, I can see the "Main" menu in the center, I can edit the text of the menu.I press the icon of main menu, a "+" icon slide out, then I can add a sub menu with press the "+". And next time, I enter this app, I can see the main menu icon around with some sub menu icons. And if I want, I can press main menu icon to hide the sub menu icon.(I cannot post a image, so I hope you can understand)

Really need help about this.

enter image description here


Solution

  • Use this code to make menu in android.And also download these icons from Android™ Drawables

    res/layout/menu.xml

    <item android:id="@+id/menu_save"
          android:icon="@drawable/icon_save"
          android:title="Save" />
    
    <item android:id="@+id/menu_search"
          android:icon="@drawable/icon_search"
          android:title="Search" />
    
    <item android:id="@+id/menu_share"
          android:icon="@drawable/icon_share"
          android:title="Share" />
    
    <item android:id="@+id/menu_delete"
          android:icon="@drawable/icon_delete"
          android:title="Delete" />  
    
    <item android:id="@+id/menu_preferences"
          android:icon="@drawable/icon_preferences"
          android:title="Preferences" />
    

    AndroidMenusActivity.java

    package com.androidhive.androidmenus;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.Toast;
    
    public class AndroidMenusActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        // Initiating Menu XML file (menu.xml)
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.layout.menu, menu);
            return true;
        }
    
        /**
         * Event Handling for Individual menu item selected
         * Identify single menu item by it's id
         * */
        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
    
            switch (item.getItemId())
            {
            case R.id.menu_bookmark:
                // Single menu item is selected do something
                // Ex: launching new activity/screen or show alert message
                Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
                return true;
    
            case R.id.menu_save:
                Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
                return true;
    
            case R.id.menu_search:
                Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
                return true;
    
            case R.id.menu_share:
                Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
                return true;
    
            case R.id.menu_delete:
                Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
                return true;
    
            case R.id.menu_preferences:
                Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
                return true;
    
            default:
                return super.onOptionsItemSelected(item);
            }
        }    
    
    }
    

    enter image description here