Dr.Snap
Dr.Snap

Reputation: 37

How to add RTL support in PreferenceScreen on Android

I want to use PreferenceScreen in my setting, but i can't set RTL to this XML file. i've tried with this code, but it doesn't work!

android:supportsRtl="true" in Manifest

minSdkVersion 15.

My XML Code :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <PreferenceCategory android:title="@string/action_settings">
        <Preference
            android:key="prefDeleteAll"
            android:summary="@string/settings_delete_all_description"
            android:title="@string/settings_delete_all" />
        <Preference
            android:key="prefCustomPath"
            android:title="@string/settings_custom_path" />
        <ListPreference
            android:defaultValue="1"
            android:entries="@array/filenameEntries"
            android:entryValues="@array/filenameValues"
            android:key="prefCustomFilename"
            android:title="@string/settings_custom_filename" />
        <ListPreference
            android:defaultValue="1"
            android:entries="@array/sortEntries"
            android:entryValues="@array/sortValues"
            android:key="prefSortMode"
            android:title="@string/settings_sort_mode" />
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/settings_customizations">
        <yuku.ambilwarna.widget.AmbilWarnaPreference
            android:defaultValue="@color/primary"
            android:key="prefPrimaryColor"
            android:summary="@string/settings_primary_color_description"
            android:title="@string/settings_primary_color" />
        <yuku.ambilwarna.widget.AmbilWarnaPreference
            android:defaultValue="@color/fab"
            android:key="prefFABColor"
            android:summary="@string/settings_fab_color_description"
            android:title="@string/settings_fab_color" />
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="prefNavigationBlack"
            android:summary="@string/settings_navigation_black_description"
            android:title="@string/settings_navigation_black" />
        <Preference
            android:key="prefDefaultValues"
            android:summary="@string/settings_default_customization_description"
            android:title="@string/settings_default_customization" />
    </PreferenceCategory>
    <PreferenceCategory android:title="@string/action_about">
        <Preference
            android:key="prefLicense"
            android:title="@string/settings_license" />
        <Preference
            android:key="prefVersion"
            android:summary="@string/settings_about"
            android:title="@string/app_name" />
    </PreferenceCategory>
</PreferenceScreen>


How to fix it?

Upvotes: 1

Views: 1533

Answers (3)

B.HoucinE
B.HoucinE

Reputation: 31

You can use this code with AppCompatPreferenceActivity:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
     getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

Upvotes: 1

Rany Albeg Wein
Rany Albeg Wein

Reputation: 3474

I override the Preference#onCreateView(ViewGroup) and applied some RTL rules to the View returned from super.

Here's an implementation of a EditTextPreferenceRTL which behaves like an EditTextPreference but its 'title' and 'summary' are being displayed from Right-To-Left:

public class EditTextPreferenceRTL extends EditTextPreference {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    EditTextPreferenceRTL(Context context, AttributeSet attrs, int defStyleAttr,
                          int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    EditTextPreferenceRTL(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    EditTextPreferenceRTL(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    EditTextPreferenceRTL(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {

        final View view = super.onCreateView(parent);

        final int titleIndex = 0;
        final int summaryIndex = 1;

        RelativeLayout layout = (RelativeLayout) ((LinearLayout) view).getChildAt(summaryIndex);
        RelativeLayout.LayoutParams lpTitle = new RelativeLayout.LayoutParams(view.getLayoutParams());
        RelativeLayout.LayoutParams lpSummary = new RelativeLayout.LayoutParams(view.getLayoutParams());

        lpTitle.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lpSummary.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        TextView tvTitle = (TextView) layout.getChildAt(titleIndex);
        tvTitle.setLayoutParams(lpTitle);

        lpSummary.addRule(RelativeLayout.BELOW, tvTitle.getId());

        TextView tvSummary = (TextView) layout.getChildAt(summaryIndex);
        tvSummary.setLayoutParams(lpSummary);

        return view;
    }
}

NOTE

This code is not forward-compatible and might break in future versions, in which there's a chance of a change in EditTextPreference from which EditTextPreferenceRTL extends.

Upvotes: 1

Yonatan Kahana
Yonatan Kahana

Reputation: 146

There is a trick how to do it, because basiclly PreferenceScreen dont support RTL.

The easiest way to do it is to put the next code in the onCreate of your PreferenceActivity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        getListView().setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);

That's should do the trick

Upvotes: 0

Related Questions