0

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 advance

here is my menu.xml

<menu xmlns: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">

<item android:id="@+id/one" android:title="one"

    app:showAsAction="never"
    />

<item android:id="@+id/two" android:title="two"
    android:orderInCategory="100" app:showAsAction="never" />

<item android:id="@+id/three" android:title="three"
    android:orderInCategory="100" app:showAsAction="never" />
</menu>
Baskar P
  • 79
  • 1
  • 2
  • 11

1 Answers1

0

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;

public class MainActivity extends Activity {

    Context context;

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

        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popupMenu = new PopupMenu(context, view);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        switch (item.getItemId()) {
                            case R.id.one:
                                Toast.makeText(getApplicationContext(), "1",
                                        Toast.LENGTH_SHORT).show();
                                return true;
                            case R.id.two:
                                Toast.makeText(getApplicationContext(), "2",
                                        Toast.LENGTH_SHORT).show();
                                return true;
                            case R.id.three:
                                Toast.makeText(getApplicationContext(), "3",
                                        Toast.LENGTH_SHORT).show();
                                return true;
                        }
                        return false;
                    }
                });
                popupMenu.inflate(R.menu.main);
                // Force icons to show
                Object menuHelper;
                Class[] argTypes;

                try {
                    Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
                    fMenuHelper.setAccessible(true);
                    menuHelper = fMenuHelper.get(popupMenu);
                    argTypes = new Class[]{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"?>
<menu xmlns: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">

    <item
        android:id="@+id/one"
        android:icon="@mipmap/ic_launcher"
        android:title="one"
        app:showAsAction="never" />

    <item
        android:id="@+id/two"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="100"
        android:title="two"
        app:showAsAction="never" />

    <item
        android:id="@+id/three"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="100"
        android:title="three"
        app:showAsAction="never" />
</menu>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • getting error in onMenuItemSelected(menu, item) in default in switch statement... – Baskar P Aug 22 '16 at 06:29
  • i am using class that extends AppCompatActivity that doesn't support onMenuItemSelected() it supports only onOptionsItemSelected()...i think thats why iam getting error at switch statement – Baskar P Aug 22 '16 at 06:49
  • i don't know how you are using that onclick method..please help me with simple code – Baskar P Aug 22 '16 at 07:35
  • @BaskarP if help you in any way please tick mark its below the vote sign :) happy coding. – Sohail Zahid Aug 22 '16 at 10:35
  • little help...i want to implement it when i click on Ic_action_overflow BUTTON (default button)...how its done..by your method – Baskar P Aug 22 '16 at 11:09