Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView error in horizontal spacing

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

like image 897
malhobayyeb Avatar asked Dec 31 '14 10:12

malhobayyeb


Video Answer


2 Answers

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);
like image 165
Amir Avatar answered Oct 19 '22 05:10

Amir


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

like image 3
box Avatar answered Oct 19 '22 05:10

box