Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create android linearlayout with RTL horizontal orientation

I want to create a linear layout with horizontal orientation

but I want it to position its children aligned to its right and not to its left as usual

how can I do this?

I know how to do this using relativeLayout, but I want to practice linearLayout

like image 818
Elad Benda Avatar asked Dec 17 '13 11:12

Elad Benda


4 Answers

You can use android:layoutDirection attribute which is introduced in 4.2(jelly bean).

The following links will help.

http://developer.android.com/reference/android/view/View.html#attr_android:layoutDirection

http://android-developers.blogspot.co.uk/2013/03/native-rtl-support-in-android-42.html

like image 200
harun Avatar answered Oct 03 '22 11:10

harun


if you are using Android studio you can do this:

1- right click on the project main folder

2- refactor

3- add support for RTL

like image 28
Fareed Alnamrouti Avatar answered Oct 03 '22 12:10

Fareed Alnamrouti


you can use this code snippet to reverse your layout views.

LinearLayout ll = // inflate
ArrayList<View> views = new ArrayList<View>();
for(int x = 0; x < ll.getChildCount(); x++) {
    views.add(ll.getChildAt(x));
}
ll.removeAllViews();
for(int x = views.size() - 1; x >= 0; x--) {
    ll.addView(views.get(x));
}

OR To begin supporting RTL layouts in your app, set the android:supportsRtl attribute to the element in your manifest file and set it “true". Once you enable this, the system will enable various RTL APIs to display your app with RTL layouts. For instance, the action bar will show the icon and title on the right side and action buttons on the left, and any layouts you’ve created with the framework-provided View classes will also be reversed. Look at this android doc

like image 34
Adhikari Bishwash Avatar answered Oct 03 '22 13:10

Adhikari Bishwash


You can set on the parent view element's gravity to right

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" <!-- make sure this is not wrap_content !-->
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="right" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"/>

</LinearLayout
like image 31
Rick Royd Aban Avatar answered Oct 03 '22 12:10

Rick Royd Aban