7

I have placed a GridView in my XML layout file.

It is displayed as I wanted in the English (LTR) on both API 17-21 But when I witch the language to Arabic (RTL) the horizontalSpacing is ignored on API 17-19 but on API 21 it is displayed as I wanted.

The same behavior occurred on emulators and real Android devices.

XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/services_home_main_layout"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/services_home_contents"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/services_home_bottom_layout">

        <GridView
            android:id="@+id/services_home_services_gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3"
            android:horizontalSpacing="4dp"
            android:verticalSpacing="4dp"
            android:layout_margin="4dp"
            android:gravity="center"
            android:choiceMode="singleChoice" />

    </LinearLayout>

    <LinearLayout
        android:id="@id/services_home_bottom_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

XML code for items:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/services_drawer_item_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:padding="4dp"
    android:background="@color/Gray_CC"
    android:drawableTop="@drawable/tall4"
    android:text="@string/app_name" />

Here is my screenshots of Arabic (RTL) UI:

Notice the horizontal spacing in the first screenshot

enter image description here

malhobayyeb
  • 2,725
  • 11
  • 57
  • 91

4 Answers4

4

try this, i don't know why but worked for me

if(activity.getWindow().getDecorView().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
    gridView.setHorizontalSpacing((int) -padding);
else
    gridView.setHorizontalSpacing((int) padding);
Amir
  • 1,250
  • 18
  • 22
  • But it's not correct... it works in below 21 but not in 21 and above. it creates problem for 21 and above, trying to solve with checking device api – karan Sep 18 '15 at 06:18
  • I also experienced this problem when i set the horizontal spacing in GridView for RTL direction. Checking the layout direction and Api check seems to work :). – box Feb 23 '16 at 10:46
  • This approach will only work if you don't have nothing on the left of each item. Otherwise it will cut your image/text/wtv, this is just pushing the item left. If that doesn't bring you any issue, then just checking API version and layout direction will work perfectly. – manuelvsc Mar 28 '16 at 16:51
3

I had the same problem and i tried to solve it but it just doesn't work as expected. If you have RTL set in your manifest then set the layout direction of the GridView to LRT and rotateY by 180.

Here is a detailed explanation: android grid view place items from right to left

Community
  • 1
  • 1
box
  • 4,450
  • 2
  • 38
  • 38
  • This should work for GridView with image only but if there is text too it might get mirrored. So Grid item also needs to be rotated by 180. – darshanz Jun 15 '16 at 20:59
  • I just set direction to `grid.setLayoutDirection(View.LAYOUT_DIRECTION_LTR)` and it worked. – Saeed Neamati May 18 '17 at 07:08
0

The issue can be fixed by setting android:layoutDirection="ltr" in the layout that contains the GridView and then, you need to create 2 layouts, one for RTL and another for LTR, and check in your adapter which type you should use.

manuelvsc
  • 525
  • 7
  • 11
-1

i tried this code

in xml:

android:horizontalSpacing="4dp"

in java:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)//V.17
{
    gView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){ //V.21
        gView.setHorizontalSpacing((int) -4);
    }
}
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Bodekjan
  • 1
  • 2