Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right align PreferencesActivity in android?

I have PreferencesActivity which I need it to be right aligned because I want to use Arabic language, I tried to use android:layout_gravity="right" for PreferenceScreen but it didn't work.

This is my XML:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="right">
        <PreferenceCategory android:title="General Settings">
                <CheckBoxPreference
                        android:title="Full Screen"
                        android:defaultValue="false"
                        android:summary="Always view as Full Screen"
                        android:key="fullScreenPref" />
        <Preference
                android:title="Report Bugs"
                android:summary="Notify us for any Bugs or Errors"
                android:key="bugs"/>
        <Preference
                android:title="About"
                android:summary="Version 1.0.0"
                android:key="about"/>
        </PreferenceCategory>
</PreferenceScreen>

This is how I use the XML inside PreferencesActivity:

addPreferencesFromResource(R.layout.preferences);
like image 428
Hesham Saeed Avatar asked Jul 04 '12 15:07

Hesham Saeed


3 Answers

After spending some time searching about our shared question, I found nothing useful. obviosly android api doesn't support RTL prefefrences. it means no support for persian/hebrew/arabic languages. But afterall I tried to find some way to right align an editText. I drived EditTextPreference class and implemented onCreateView method again. here is my code:

/**
 * Created by omid on 1/12/14.
 */
public class RtlTextPreference extends EditTextPreference {


    public RtlTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        View view = super.onCreateView(parent);

        RelativeLayout layout = (RelativeLayout) ((LinearLayout) view).getChildAt(1);
        layout.setGravity(Gravity.RIGHT);
        return view;
    }
}

the rest of the preferences in the standard android api should be the same(btw I haven't tried yet). maybe someday we should fully implement rtl support in android preferences and host it on github. Hope this code snippet helps you.

like image 68
omid.n Avatar answered Nov 03 '22 17:11

omid.n


Unfortunately the base Preference components (PreferenceScreen, Preference, etc.) are implemented in a fairly non-configurable way. They are designed to work in a specific way and are not terribly customizable.

In order to do what you want, you will probably have to implement your own version of PreferenceScreen (or possible just the Preference types you are using such as CheckBoxPreference). The way it is designed, when it inflates XML layouts it assumes a lot of things about them and handles it for you (text size, padding, layout, wrapping, etc). This is great if you want it to look like the default, but not so great if you need to tweak these configurations.

(note: if you don't want to implement preferencescreen you could just use a listview and treat it as a preference page. However, either way you basically have to implement your own version of preferences.)

(note2: based on other questions I've seen about arabic text, it's POSSIBLE android is smart enough to try to right align it by default. However, I would be surprised if this was the case. Still, it's worth a try.)

Sorry I don't have a better answer for you.

like image 8
matt5784 Avatar answered Nov 03 '22 18:11

matt5784


In your manifest file under application tag add this line,

android:supportsRtl="true"

like image 5
Omar Hassan Avatar answered Nov 03 '22 19:11

Omar Hassan