Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the direction of a popup menu Item from right to left?

I know this question was asked many times and I used many solutions but it doesn't work for me . I want to change the direction of my Menu to rtl using a Popup but It doesn't work .

my menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layoutDirection="rtl">
<item
    android:id="@+id/more"
    android:title="@string/menue" />
<item
    android:id="@+id/rate"
    android:title="@string/rate"/>
<item
    android:id="@+id/share"
    android:title="@string/share" />

my popup

imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu popup = new PopupMenu(MainActivity.this, imageButton );
            popup.getMenuInflater().inflate(R.menu.app_menue, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        case R.id.more:
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.setData(Uri.parse(res.getString(R.string.moreApps)));
                            startActivity(intent);
                            break;
                        case R.id.rate:
                            Intent intent1 = new Intent(Intent.ACTION_VIEW);
                            intent1.setData(Uri.parse(res.getString(R.string.rateApp)));
                            startActivity(intent1);
                            break;
                        case R.id.share:
                            Intent myIntent = new Intent(Intent.ACTION_SEND);
                            myIntent.setType("text/plain");
                            String shareBody = "Share our app";
                            myIntent.putExtra(Intent.EXTRA_SUBJECT, shareBody);
                            myIntent.putExtra(Intent.EXTRA_TEXT, res.getString(R.string.shareApp));
                            startActivity(Intent.createChooser(myIntent, "Share using"));
                            break;
                    }
                    return true;
                }
            });
            popup.show();
        }
    });

And in my Manifest

    android:supportsRtl="true"
like image 259
Ahmed Avatar asked Apr 18 '18 13:04

Ahmed


1 Answers

I found a solution .

Making my own style like this

<style name="menuStyle">
<item name="android:layoutDirection">rtl</item>
</style>

Then add this code

Context myContext = new ContextThemeWrapper(MainActivity.this,R.style.menuStyle); 
PopupMenu popupMenu = new PopupMenu(myContext , myView);
like image 176
Ahmed Avatar answered Nov 14 '22 23:11

Ahmed