2

On Click of button, I'm showing the menu which contains the list of languages. Now i want the icon to be displayed at the right side of title of every item in the menu. I googled but did not find appropriate solution , please help me out.

menu_language.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/nav_arabic"
        android:title="Arabic" />
    <item
        android:id="@+id/nav_southAfrica"
        android:icon="@drawable/ic_earphones"
        android:title="Afrikaans"
        />
    <item
        android:id="@+id/nav_albania"
        android:icon="@drawable/ic_earphones"
        android:title="Albanian " />
    <item
        android:id="@+id/nav_armenian"
        android:title="Armenian" />
    <item
        android:id="@+id/nav_azerbaijani"
        android:icon="@drawable/ic_earphones"
        android:title="Azerbaijani" />
    <item
        android:id="@+id/nav_bangla"
        android:icon="@drawable/ic_earphones"
        android:title="Bangla" />
    <item
        android:id="@+id/nav_basque"
        android:icon="@drawable/ic_earphones"
        android:title="Basque" />
    <item
        android:id="@+id/nav_belarusian"
        android:title="Belarusian" />

Language.java:

 public void showMenu(View v) {
        PopupMenu popup = new PopupMenu(getActivity(), v);

        Object menuHelper;
        Class[] argTypes;
        try {
            Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
            fMenuHelper.setAccessible(true);
            menuHelper = fMenuHelper.get(popup);
            argTypes = new Class[]{boolean.class};
            menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
        } catch (Exception e) {

        }

        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu_language, popup.getMenu());
        popup.setOnMenuItemClickListener(this);

As of now the icon is coming at left in menu but i want it to be at the right side of title . please help , any clue?

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • hie murphy! you are using builtin menu xml file to have a popup menu! which is by default with menuIcon always on left property as they can expand the label on right according to screen size – Rizwan Atta Jul 24 '18 at 10:42
  • here is what you can do! you can just have a custom layout file designed as your on in layout folder for menu ! and after that you get the menu inflator and ask it to null down old menu and add your custom layout there! ? want an example of code? – Rizwan Atta Jul 24 '18 at 10:43
  • or simply contexrMenu which pops up on longPress has options for you to show what every you want! but if you are persistent with optionMenu you have to go with my solution – Rizwan Atta Jul 24 '18 at 10:44
  • [enter link description](http://stackoverflow.com/questions/47071568/how-to-set-navigation-drawer-icons-to-the-right-side-of-the-item-texts). try this – Swapna Jul 24 '18 at 10:45
  • @Rizwanatta Thank you , yes It will be more clear if you share example. –  Jul 24 '18 at 11:26
  • @Rizwanatta can you please give me example of your solution? –  Jul 24 '18 at 12:06
  • see https://medium.com/keepsafe-engineering/building-a-custom-overflow-menu-aaa09b0b9054 , if this is what you need! a nice reference for customising your menus – Rizwan Atta Jul 24 '18 at 12:19
  • @Rizwanatta this is what I'm doing right now but icon coming at left of title , i want icon to be at right . –  Jul 24 '18 at 12:27

1 Answers1

0

For custom layouts you can't use a menu, one alternate option is a PopupWindow which helps you to display an arbitrary window with your custom views which you can style and use it as menus

do something like this

PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);

public PopupWindow popupDisplay() 
{ 

    final PopupWindow popupWindow = new PopupWindow(this);

    // inflate your layout or dynamically add view
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View view = inflater.inflate(R.layout.mylayout, null);

    TextView item = (TextView) view.findViewById(R.id.button1);

    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);

    return popupWindow;
}

create an xml file like this

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/button1"
        drawableRight='@drawbale/icon'
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Window test" />
</LinearLayout>
Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31
  • why do we need xml file? and where i need to inflate my menu? –  Jul 24 '18 at 12:40
  • you can't customise the menu file its prebuilt! but what you can do is just make a custom design menu option alike file of xml and inflate it! – Rizwan Atta Jul 24 '18 at 12:42
  • but I have like thousand of item in it so in that way it would very large xml –  Jul 24 '18 at 12:43
  • better option for that would be like having a popupOpened with a spinner in it ! having the language names and images ! and you can fill that from your String-array from strings.xml or maybe using java too – Rizwan Atta Jul 24 '18 at 12:47
  • Yeah , but is there any option for menu to have icon at the right side? man only icon we have to shift why is it so difficult . –  Jul 24 '18 at 12:53
  • sadly nope! this is the only way that I know or I read the whole doc but there is nothing there and also I think in perspective of HCI that is not a right approach either! but the way I showed above is the one which can help – Rizwan Atta Jul 24 '18 at 13:08