Home » Android » android – Start the GridView Elements from the right

android – Start the GridView Elements from the right

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am making an app where i want the gridview to be filled from the right to the left “to use it for right to left languages”, i tried to make the gridView gravity set to right but it didn’t work … my gridview layout is below.

Thanks in advance

<RelativeLayout
    android:id="@+id/grid_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/show_info"
    android:gravity="right" >

    <GridView
        android:id="@+id/channel_grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:columnWidth="90dp"
        android:gravity="right"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>
</RelativeLayout>
How to&Answers:

I think your best guess is to copy the code from the GridView source and change the measuring/layouting to use right to left.

UPDATE: The layout direction for views was only introduced in API Level 17 (4.2).
If you compare GridView.java in 4.2 and 4.1, you can see that for example in the method makeRow(), the layoutDirection is taken into account since 4.2, so that should work!

Did you try to execute your code with android:layoutDirection="rtl" on API Level 17 ?

Answer:

I don’t think GridView was designed having languages in mind. It is more of a visual component for arranging thumbnail images.

You could solve that problem by modifying your adapter and letting it reverse the order of each item per row. However, you’d need to tell your adapter the amount of items per row for making that work.

On top of that if the last row has less items than possible, then you need to introduce empty dummy items for filling up the left.

Answer:

You could try this. Have your GridView in your layout file have the following attribute

android:layoutAnimation="@anim/layout_grid_right_to_left"

Create an anim folder wherein you add the layout_grid_right_to_left.xml with cotents

<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" />

Also add fade.xml in the same anim folder with contents

<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" />

These are just for your reference. Please feel free to add/remove attributes that suit your needs/requirements.

Answer:

You can try

android:layoutDirection="rtl"

It is usually for text and I don’t know what’s the effect on a gridView. (ref)

Another thing to try is

android:stackFromBottom="true"

But it won’t work if you need to order the items top-down and right-to-left (ref)

Answer:

You can try this for your gridview and all of its items in XML :

 android:rotationY="180"

You can also use android:layoutDirection="rtl". It may solve your problem.

Answer:

There is a silly way to do this but it’s work perfectly.

just make android:ScaleX=-1 in gridview and in your item container(your custom view for grid view cells), so your layout become start from Right to Left!

hope this help