Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to RTL this android layout?

I want to rtl (Right To Left) below layout in android like when using layoutDirection="rtl" in parent layout (but it only for 4.2 and higher) and any way to make it direction to right-to-left clearly. This is a part of main android project layout which used for Preference Activity in android.

<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
Layout for a Preference in a PreferenceActivity. The
Preference is able to place a specific widget for its particular
type in the "widget_frame" layout.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingEnd="?android:attr/scrollbarSize" >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginEnd="6dip"
        android:layout_marginStart="15dip"
        android:layout_marginTop="6dip"
        android:layout_weight="1" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary" />
    </RelativeLayout>
    <!-- Preference should place its actual preference widget here. -->

    <LinearLayout
        android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>

(Sorry for my weak English!!)

like image 225
Mokhtarabadi Avatar asked Dec 04 '14 05:12

Mokhtarabadi


People also ask

How do I change RTL on android?

Just go to Android Studio > Refactor > Add RTL support where possible… I would recommend you checking your app once after applying this change as you might not want all your Layouts/Views to be RTL. If you want to force any layout to LTR then just add android:layoutDirection="ltr" to that view.

What is LTR and RTL in android?

So when you setted an RTL script text to textview, text will start from right automatically. But if in RTL mode you set an LTR script text, textView's text will start left like below (Even if you replaced left/right to start/end).


1 Answers

Unfortunately, RTL layout support feature is supported on Android 4.2(API level 17) or above only. For the versions before, you can add a layout folder for RTL layout. For example in layout folder you put LTR version files layout and in layout-ldrtl folder you put RTL version files layout.

As of what to change to make RTL it depends on how much you would like to make it in RTL. because some parts of view will remain on the same side (like navigation bar) unless you customize it. Other things you will have to see and change what should be changed to make it cool view. For example on the LTR view lets say we have this:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:orientation="horizontal"
    android:paddingLeft="8dp"
    android:paddingRight="4dp">
</LinearLayout>

Then on the RTL view you will be somethig like this:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:orientation="horizontal"
    android:paddingLeft="4dp"
    android:paddingRight="8dp">
</LinearLayout>

As you can see we have changed the padding in the android:paddingLeft="4dp" and android:paddingRight="8dp" but we kept the android:layout_gravity="left" because it should be like that for this case (maybe on another view will be android:layout_gravity="right").

As real life example, you can compare the two views of the Android Quran app RTL audio_panel and LTR audio_panel.

like image 93
xsoh Avatar answered Sep 21 '22 16:09

xsoh