Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting MenuItem icon from URL - Android

I want to set a MenuItem in my ActionBar which leads to the user profile page in my app. I would like the icon for that to be his profile picture for which I have the URL and can create a BitMap out of.

The image isn't stored in my project folder or anywhere locally so I can't pick it up from R.drawable.

Can somebody help me with setting a bitmap created with the URL as the MenuItem icon? Thanks for the help!

like image 589
Mallika Khullar Avatar asked Oct 18 '25 13:10

Mallika Khullar


2 Answers

You could do something like this to set the icon from a bitmap:

myMenuItem.setIcon(new BitmapDrawable(getResources(), myBitmap));

In your code this would looks a bit like this:

public boolean onCreateOptionsMenu( Menu menu ) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate( R.menu.actionbar, menu );
    userItem = menu.findItem(R.id.userItem);

    Bitmap myBitmap = //get your bitmap
    userItem.setIcon(new BitmapDrawable(getResources(), myBitmap));

    return menu;
}

You'll need to get the file from the URL and turn it into a Bitmap first. Note that this will be slow, since if you are doing this when your app is started, the user will have to wait until the file is downloaded before the app will be shown. If your icon changes infrequently, I'd recommend caching it on the device and reusing the locally stored copy.

Also check the "Changing the menus at runtime" section here.

like image 145
JonasCz Avatar answered Oct 20 '25 03:10

JonasCz


I was searching for this as well and recently I found it from another stackoverflow answer which I'm giving reference link This one is for JAVA and I'm adding Kotlin code below.

Here is the code for kotlin code: imageUser is Url of image in string format. You should add it your own image url.

Glide.with(this).asBitmap().load(imageUser)
                    .into(object : SimpleTarget<Bitmap?>(150, 100) {
                        override fun onResourceReady(
                            resource: Bitmap,
                            transition: Transition<in Bitmap?>?
                        ) {
                            yourItemIcon.setIcon(BitmapDrawable(resources, resource))
                        }
                    })

Set your item as below inside the. onCreateOptionsMenu

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu_main, menu)
    val yourItemIcon  = menu!!.findItem(R.id.ic_topic_info)     
  return true

}

For new users: You should also add these lines of code to dependencies the build.gradle in app level for adding Glide library in your app.

implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
like image 20
profiile_samir Avatar answered Oct 20 '25 02:10

profiile_samir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!