How can I create an android menu item using android setting icon
Asked Answered
D

4

42

Can you please tell me how can I create an android menu item using android setting icon?

Dolliedolloff answered 8/6, 2010 at 17:50 Comment(2)
What is "android setting icon"?Boraginaceous
I'm sure he is referring to gear icons like theseCarder
S
61

Here is a list of the standard icons. I don't see a "settings" icon. Perhaps you mean "Preferences" (ic_menu_preferences)?

You can set the icon programmatically like this:

menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);

You can also set it in your xml layout like this:

<item android:id="@+id/save_button"
      android:icon="@android:drawable/ic_menu_save"
      android:title="Save Image"/>
Sherbrooke answered 8/6, 2010 at 18:0 Comment(4)
+1 thanks, I was looking for how to reference the drawable from xml.Arcograph
The code xml, not work correctly in Material Design (2015) and java, the line is created of other form "getMenuInflater(),.."Grits
Must be missing something. Not material design, did the above, and I see no settings icon. Shows up in Android studio in the code, but not on the screen!Donetta
For me referring to a private icon works without android prefix e.g. android:icon="@drawable/abc_ic_menu_share_mtrl_alpha". It worked even when I had not copied the image in my project. I'm wondering how it is possible to refer images of Android SDK without android prefix which you've mentioned in your post.Carder
M
9

You can see all the icons in the android SDK forder:

_your_install_path_\android-sdk\platforms\android-10\data\res\drawable-hdpi\

and then get a reference to them with:

android.R.drawable.ic_menu_preferences

just like it was your drawable.

Martelli answered 2/7, 2012 at 13:58 Comment(1)
Worth mentioning: some of them aren't public, thus unusable.Partnership
L
7

Add a new Vector Asset.

  1. File -> New -> Vector Asset.

enter image description here

  1. Click on the icon to change it.

enter image description here

  1. Select the icon you want (e.g. search for "setting").

enter image description here

  1. Adjust other settings.

  2. Use that new Vector Asset in your xml.

    android:logo="@drawable/ic_settings_white_24dp"
    
  3. Party!

enter image description here

Luminal answered 11/10, 2019 at 19:50 Comment(0)
C
1

If you want to handle the event, just try this on your activity

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // action with ID action_refresh was selected
            case android.R.drawable.ic_popup_sync:
                Toast.makeText(this, "ic_popup_sync selected", Toast.LENGTH_SHORT)
                        .show();
                break;
            default:
                break;
        }

        return true;
    }

And in your menu folder use something like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.test.app.MainActivity"
    >

    <item android:id="@+id/action_settings1"
        android:icon="@drawable/abc_ic_search"
        android:title="Find Location"
        android:orderInCategory="100"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/save_button"
        android:icon="@android:drawable/ic_menu_save"
        android:title="Save Image"/>

    <item android:id="@+id/refresh"
        android:icon="@android:drawable/ic_popup_sync"
        android:title="Refresh"/>


</menu>
Cockaleekie answered 11/7, 2014 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.