Skip to content Skip to sidebar Skip to footer

How To Implement Image With Text In Menu Options In Android

i am trying so hard to implement customized menu option with both image and text in menu item list but i did not get it...so please help me how to implement it...thank you in advan

Solution 1:

You can use PopupMenu on any view clicked.

Full Demo:

MainActivity:

package pk.sohail.gallerytest.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

import java.lang.reflect.Field;

import pk.sohail.gallerytest.R;

publicclassMainActivityextendsActivity {

    Context context;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        Buttonbtn= (Button) findViewById(R.id.button);
        btn.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                PopupMenupopupMenu=newPopupMenu(context, view);
                popupMenu.setOnMenuItemClickListener(newPopupMenu.OnMenuItemClickListener() {
                    @OverridepublicbooleanonMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stubswitch (item.getItemId()) {
                            case R.id.one:
                                Toast.makeText(getApplicationContext(), "1",
                                        Toast.LENGTH_SHORT).show();
                                returntrue;
                            case R.id.two:
                                Toast.makeText(getApplicationContext(), "2",
                                        Toast.LENGTH_SHORT).show();
                                returntrue;
                            case R.id.three:
                                Toast.makeText(getApplicationContext(), "3",
                                        Toast.LENGTH_SHORT).show();
                                returntrue;
                        }
                        returnfalse;
                    }
                });
                popupMenu.inflate(R.menu.main);
                // Force icons to show
                Object menuHelper;
                Class[] argTypes;

                try {
                    FieldfMenuHelper= PopupMenu.class.getDeclaredField("mPopup");
                    fMenuHelper.setAccessible(true);
                    menuHelper = fMenuHelper.get(popupMenu);
                    argTypes = newClass[]{boolean.class};
                    menuHelper.getClass()
                            .getDeclaredMethod("setForceShowIcon", argTypes)
                            .invoke(menuHelper, true);
                } catch (Exception e) {
                    // Possible exceptions are NoSuchMethodError and// NoSuchFieldError//// In either case, an exception indicates something is wrong// with the reflection code, or the// structure of the PopupMenu class or its dependencies has// changed.//// These exceptions should never happen since we're shipping the// AppCompat library in our own apk,// but in the case that they do, we simply can't force icons to// display, so log the error and// show the menu normally.

                    Log.w("as", "error forcing menu icons to show", e);
                    popupMenu.show();
                    return;
                }

                popupMenu.show();
            }
        });

    }
}

activity_main.xml:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pop Menu"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

R.menu.main:

<?xml version="1.0" encoding="utf-8"?><menuxmlns: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.admin.sst.Signuplogin"><itemandroid:id="@+id/one"android:icon="@mipmap/ic_launcher"android:title="one"app:showAsAction="never" /><itemandroid:id="@+id/two"android:icon="@mipmap/ic_launcher"android:orderInCategory="100"android:title="two"app:showAsAction="never" /><itemandroid:id="@+id/three"android:icon="@mipmap/ic_launcher"android:orderInCategory="100"android:title="three"app:showAsAction="never" /></menu>

Post a Comment for "How To Implement Image With Text In Menu Options In Android"