Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MenuItem icon is not displaying correctly in RTL Layouts

In RTL, option menu item icon not displayed correctly!! but in LTR, everything is displayed well and beautifully.

With the help of this command, I make the RTL program

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

My menu layout.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"
    android:gravity="start"
    android:layoutDirection="rtl"
    android:layout_gravity="start">
    <item
        android:id="@+id/action_more"
        android:icon="@drawable/ic_add_white_24dp"
        android:title=""
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/action_settings"
                android:icon="@drawable/ic_wb_sunny_black_24dp"
                android:title="آیتم شماره 1"/>

            <item
                android:id="@+id/action_settings2"
                android:icon="@drawable/ic_star_black_24dp"
                android:title="آیتم شماره 2"/>

            <item
                android:id="@+id/action_settings3"
                android:icon="@drawable/ic_wb_sunny_black_24dp"
                android:title="آیتم شماره 3"/>
        </menu>
    </item>
</menu>

Please help me to fix this.

enter image description here Screenshot

like image 697
Shahram Avatar asked Oct 21 '25 07:10

Shahram


1 Answers

I faced the same problem in RTL layout, If you are using Support Library or AndroidX you can apply this easy fix:

  1. In you project create new layout file in res -> layout with this name abc_list_menu_item_icon.xml.
  2. Copy the code below to the file in step 1.

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2007 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
    
              http://www.apache.org/licenses/LICENSE-2.0
    
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
               android:id="@+id/icon"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"
               android:layout_marginLeft="8dip"
               android:layout_marginRight="-8dip"
               android:layout_marginStart="8dip"
               android:layout_marginEnd="-8dip"
               android:layout_marginTop="8dip"
               android:layout_marginBottom="8dip"
               android:scaleType="centerInside"
               android:duplicateParentState="true"/>
    
like image 64
Motaz Alnuweiri Avatar answered Oct 22 '25 22:10

Motaz Alnuweiri



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!