Home » Android » java – add image to menu item

java – add image to menu item

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have .png images in my res/drawable-hdpi/delete.png I want to add it as icon to my menu item.

I used:

delete.xml

<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/deletebutton">
</bitmap>

Menu activity_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/create"
    android:showAsAction="always"
    android:title="@string/createTask"/>
<item
    android:id="@+id/delete"
    android:showAsAction="always"
    android:icon="@drawable/delete"
    android:title="@string/deleteTask" />
</menu>

But its not working.

I removed the xml file and tried directly to load the image here is the log file.

04-02 17:46:50.887: E/AndroidRuntime(11957): FATAL EXCEPTION: main
04-02 17:46:50.887: E/AndroidRuntime(11957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.devicecontrolpanel/com.example.devicecontrolpanel.Main}: android.content.res.Resources$NotFoundException: Resource ID #0x7f020008
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.os.Looper.loop(Looper.java:156)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.main(ActivityThread.java:4987)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at java.lang.reflect.Method.invoke(Method.java:511)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at dalvik.system.NativeStart.main(Native Method)
04-02 17:46:50.887: E/AndroidRuntime(11957): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020008
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.content.res.Resources.getValue(Resources.java:1105)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.content.res.Resources.getDrawable(Resources.java:674)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.widget.CompoundButton.setButtonDrawable(CompoundButton.java:189)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.example.devicecontrolpanel.Main.listAllAlarms(Main.java:69)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.example.devicecontrolpanel.Main.onCreate(Main.java:25)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.Activity.performCreate(Activity.java:4538)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
04-02 17:46:50.887: E/AndroidRuntime(11957):    ... 11 more
How to&Answers:

At first you really don’t need delete.xml in your case.

<item
    android:id="@+id/delete"
    android:showAsAction="always"
    android:icon="@drawable/deletebutton"
    android:title="@string
/>

And also i think you have problem here:

android:title="@string

Here, you are missing right apostrophe and value. If you don’t want to show title in MenuItem just remove this property.

Update:

java.lang.StackOverflowError

You have SO at loadDrawable() and it means that your Drawable is too big to load(you stack is filled up completely). Try to use another(smaller) or trim actual one. And also make sure you have your image in all drawable folders.

Answer:

It’s overdue but I’ll Give an answer just for future reference by others, you can do further styling programatically like so:

public class MyClass {
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        // Do your other stuff here

        MenuItem item = menu.getItem(0);

        if (item.getItemId() == R.id.action_example) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setMaxHeight(18);
            imageView.setMaxWidth(18);
            imageView.setImageResource(R.drawable.barchart32);

            item.setActionView(imageView);
        }
    }
}

Answer:

android:background="@drawable/ic_play" /> is what i am doing,

Answer:

Have a look at this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/open" android:icon="@drawable/openicon"
        android:title="Open the file" />

    <item
        android:id="@+id/save"
        android:icon="@drawable/saveicon"
        android:title="Save the file" />
</menu>

There is no need to set bitmap files, just use above lines alone. Hope this will help you.

Follow this tutorial Menus in Android