Ravneet
Ravneet

Reputation: 29

Unable to display menu icons in android application

Please see below the menu.xml file I have created , which I have kept under res-> menu-> folder

 <item android:id="@+id/menu_About"
      android:icon="@drawable/icon_about"
      android:title="@string/About" />

<item android:id="@+id/menu_Settings"
      android:icon="@drawable/icon_settings"
      android:title="@string/Settings" />

<item android:id="@+id/menu_Exit"
      android:icon="@drawable/icon_exit"
      android:title="@string/Exit" />   

I have kept the icon_about.png , icon_settings.png , icon_exit.png files under res-> drawable-hdpi ,drawable-ldpi , drawable-mdpi , drawable-xhdpi, drawable-xxhdpi folders but when my application runs, these icons don't show up. Please tell me if I am missing something.

Thanks

Upvotes: 1

Views: 905

Answers (5)

Pinki
Pinki

Reputation: 21929

Try to use this in your activity:

@Override   
public boolean onCreateOptionsMenu(Menu menu) { 
    Inflate the menu; // this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);       
    return true;    
}

Upvotes: 1

Mayur Raval
Mayur Raval

Reputation: 3275

<item
    android:id="@+id/web_link"
    android:icon="@drawable/menu_About"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="Web link"/>

You have to add android:showAsAction="always" if you want always..there other option too. like "never", which used to never show the icon if you want they can add programatically,and other "ifroom" if there are place than this menu item will appear

Upvotes: 0

Amritha
Amritha

Reputation: 778

open your main Activity class file and type following code. In the following code each menu item is identified by its ID in switch case statement.

public class MenusActivity 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_About:

            Toast.makeText(MenusActivity.this, "about is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_settings:
            Toast.makeText(MenusActivity.this, "settings is Selected", Toast.LENGTH_SHORT).show();
            return true;


        case R.id.menu_Exit:
            Toast.makeText(MenusActivity.this, "Exit is Selected", Toast.LENGTH_SHORT).show();
            return true;



        default:
            return super.onOptionsItemSelected(item);
        }
    }    

}

. Finally run your project by right clicking on your project.On Android Emulator click on Menu Button to launch menu.

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

I have kept the icon_about.png , icon_settings.png , icon_exit.png files under res-> drawable-hdpi ,drawable-ldpi , drawable-mdpi , drawable-xhdpi, drawable-xxhdpi folders but when my application runs, these icons don't show up. Please tell me if I am missing something.

=> You haven't included android:showAsAction attribute in all the menu items.

Upvotes: 0

stinepike
stinepike

Reputation: 54672

check if you have used the icons of correct sizes. for option menus the desired sizes are

ldpi 32 x 32 px
mdpi: 36 x 36 px
hdpi: 48 x 48 px
xhdpi: 64 x 64 px

Upvotes: 0

Related Questions