Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add image to menu item

Tags:

java

android

xml

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
like image 278
Sharda Singh Avatar asked Apr 02 '13 11:04

Sharda Singh


2 Answers

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

like image 71
itsrajesh4uguys Avatar answered Oct 06 '22 01:10

itsrajesh4uguys


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.

like image 24
Simon Dorociak Avatar answered Oct 06 '22 01:10

Simon Dorociak