1

I'm trying to add an image to menu item programmatically using glide.

main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_profile_pic"
    android:icon="@mipmap/profile_pic"
    android:orderInCategory="100"
    android:title=""
    app:showAsAction="always" />

Java

@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, menu);

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.mipmap.profile_pic);
    requestOptions.error(R.mipmap.profile_pic);

    MenuItem itemProfilePic = menu.getItem(0);
    //View view = findViewById(R.id.action_profile_pic);

    if (savedProfilePic != null) {
        Glide.with(this)
                .setDefaultRequestOptions(requestOptions)
                .load(Uri.parse(savedProfilePic))
                .into(itemProfilePic);
    }
    return true;

However, I can't use this approach since the resource is a MenuItem and not an ImageView. Casting it to an ImageView also doesn't work.

How can I achieve the desired result, given that the image is not in the drawables folder, its set by the user, hence below approach also can't be used:

itemProfilePic.setActionView(savedProfilePic); Takes in an integer value

Final result

enter image description here

Boron
  • 99
  • 10
  • 34
  • so basically you require the complete row of menu to be custom or just image? – KOTIOS Jan 14 '19 at 05:07
  • I've edited the post, custom image at the end – Boron Jan 14 '19 at 05:13
  • @lbra, try this https://stackoverflow.com/questions/19882443/how-to-change-menuitem-icon-in-actionbar-programmatically – ॐ Rakesh Kumar Jan 14 '19 at 05:14
  • Possible duplicate of [How to change MenuItem icon in ActionBar programmatically](https://stackoverflow.com/questions/19882443/how-to-change-menuitem-icon-in-actionbar-programmatically) – Hanzala Jan 14 '19 at 05:20
  • @lbra try above suggested answer if that works for you – KOTIOS Jan 14 '19 at 05:21
  • @KOTIOS no it does not, my image is not in the drawable its applied by the user(custom) – Boron Jan 14 '19 at 05:41
  • @Hanzala it's not a duplicate, with the suggested answer, the OP has their image inside the drawable, unlike in my case – Boron Jan 14 '19 at 05:43
  • 1
    @lbra Is menu contains only 1 item with custom image? In that case why dont you have custom toolbar with imageview on right side? – KOTIOS Jan 14 '19 at 05:47
  • @Ibra Then what's difference in your question? even you are setting an image which is saved ... and could be converted as drawable.... – Hanzala Jan 14 '19 at 05:50

2 Answers2

1

The MenuItem Interface has a method: setIcon(Drawable d).

  1. Get a reference to the MenuItem you want to update.
  2. Convert the image you want to set into a BitmapDrawable.
  3. Adjust the drawable as desired and set via MenuItem.setIcon() method.

It seems you can also implement custom target from the image loader library you are using. The custom target can wrap a MenuItem.

Note: Since you are inflating an XML menu, chances are it will be refreshed. Therefore, perform the image icon update after inflating the XML as well.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0

make a custom toolbar and then you can use the image view to change image dynamically

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    app:contentInsetStart="0dp"
    android:background="@color/primaryColor"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:src="@drawable/ic_perm_24dp"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>


</android.support.v7.widget.Toolbar>
End User
  • 792
  • 6
  • 14