18

I'm working on an android application with arabic version.

In one of the interfaces, I have gridView. So to display items in the correct order, I have to display items in the GridView from the right to the left (and of corse from the top to the bottom). To do that, I tried to add these attributes in the GridView :

android:gravity="right"
android:layout_gravity="right"

Unfortunately, items still displayed from the left to the right.

any idea to do it in the right way ?

traninho
  • 1,511
  • 11
  • 19
ghost rider3
  • 448
  • 5
  • 16

6 Answers6

49

You can achieve this by this ugly workaround:

  1. add this line to your GridView in XML:

    android:rotationY="180"

  2. also add the same line to your GridView item in XML:

    android:rotationY="180"

In total it gives you rotation by 360° so it looks like GridView places items from right to left.

traninho
  • 1,511
  • 11
  • 19
  • thank you, it solve the problem, but you can't click on items, may be it's du to my implementation. any way I got what i need – ghost rider3 Sep 24 '13 at 12:55
  • thank you very much for this solution, but I also (like ghost rider) have problems with OnItemClickListener, it get's confused what to click... when I just remove rotationY property - it starts working as expected. Do you know smth about how to solve it? – Artem Svystun Jan 18 '14 at 13:51
  • 1
    I dont know why are you experienced issue with onItemClick. But you can make your own OnItemClickListener. Just in getView method of your adapter, set OnClickListener to your item layout and you will get OnClick events in this listener. – traninho Jan 23 '14 at 19:42
  • It is really a great idea, Thank you very much – Heysem Katibi Aug 27 '14 at 12:49
  • This solution has a concomitant problem: If you want to navigate in GridView using keyboard. Pushing left button will get you right, and vice versa. – Behnam Oct 22 '14 at 06:50
  • @Campiador yes you are right. As I mentioned, this solution is not the best. But you can intercept the keyboard clicks and switch the right key with the left key and vice versa. – traninho Oct 23 '14 at 11:49
  • @traninho: Thank you. If, by any chance, you came across another solution, please let me know. – Behnam Oct 23 '14 at 12:15
  • Awesome trick, i had a problem with horizontal spacing in RTL and this solved my issues. – box Feb 23 '16 at 14:51
  • When user DPad to navigate, I have to press the opposite button to scroll horizontally. – Ashutosh Sagar Jul 02 '20 at 07:02
6

add this line to your GridView in XML:

android:layoutDirection="rtl"
Pooya Hayati
  • 79
  • 1
  • 3
2

Based on traninho's answer:

  1. add this line to your GridView in XML:

    android:rotationY="180"

  2. also add the same line to your GridView item in XML:

    android:rotationY="180"

do the following:

  • On your GridActivity, delete this:

        gridView.setOnClickListener(...);
    
  • Now, go to your gridAdapter and add onClickListener to the gridImage so, your code should look like the following:

        gridImage.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    //Do something
    
                }
            });
    

This will handle clicks normally on your GridView images.

Wish that helps :)

blueware
  • 4,309
  • 1
  • 32
  • 59
1

As Nibha Jain said, its right. Also you need to add a property "android:layoutAnimation" to the GridView xml as below.

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/grid"    
    android:layoutAnimation="@anim/layout_grid_right_to_left"

and also you need to define an animation file with "android:direction" attribute, which actually renders your items from right to left or any supported direction.

The layout_grid_right_to_left.xml file inside anim folder

<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:columnDelay="0.5"
    android:directionPriority="row"
    android:direction="right_to_left"
    android:animation="@anim/fade" />

the @anim/fade is as below

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:fromAlpha="0.0" android:toAlpha="1.0"
   android:duration="@android:integer/config_longAnimTime" />

Its just my preferences. Please add/remove attributes that suites your needs. Play with it.

maghii
  • 11
  • 4
  • My problem is not about the direction of annimation. It's about the order of items in the GridViw : ( 1 2 3 ..) --> (.. 3 2 1) – ghost rider3 Jul 24 '13 at 10:20
0
Create one new layout-ldrtl folder and make xml file grid view .

example...
  <GridView
        android:id="@+id/popoulat_category_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollingCache="true"
        android:smoothScrollbar="true"
        android:numColumns="2"
        android:scrollbars="none"
        android:horizontalSpacing="-5dp"
        android:verticalSpacing="-5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

device local language is English : enter image description here

device local language is parsian : enter image description here

  • does this answer provide anything new that the existing ones don't have? If yes, please explain the difference, if no, why did you answer? – Florian Koch Mar 03 '17 at 10:21
  • hello i have facing same problem so Arebic language is support RTL (Right -to-left).so i will try this code and solve it. – Rahul Patil Mar 03 '17 at 11:08
0

In addition to the above answer:

android:layoutDirection="rtl"

Ideally, if you want to make it rtl aware, so only rtl in locales where it should be:

android:layoutDirection="locale"

You can also make it follow the containing layout

android:layoutDirection="inherit"
JoeHz
  • 2,035
  • 1
  • 17
  • 27