29

It appears that if you have an EditText on android with the

android:inputType="textPassword" or android:password="true

fields on them, right-to-left text does NOT appear right-to-left (stays left-to-right).

However without the password denotations the text does appear RTL.

Is this a known issue or is there a workaround?

Vic Vuci
  • 6,993
  • 6
  • 55
  • 90

6 Answers6

79

For 17+ (4.2.x+) you can use textAlignment

android:textAlignment="viewStart"

eveliotc
  • 12,863
  • 4
  • 38
  • 34
  • For 4.2.x+, yes. This still doesn't exist in the appcompat libraries, so if you want to do this in 2.3 or 4.0, you have to use the other solution. – Vic Vuci Aug 26 '14 at 17:25
  • 2
    This didn't work for me , I reported the issue at: https://code.google.com/p/android/issues/detail?id=201471&q=rtl%20password&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened – yehyatt Feb 21 '16 at 11:53
  • Almost worked. For me it displays everything fine, It just places the cursor after the hint when in left to right layout – Tooroop Sep 28 '16 at 11:07
  • Switching this to the answer now that 4.2+ has taken majority. – Vic Vuci Feb 28 '17 at 18:13
  • It's worth to mention that this requires having `android:supportsRtl="true"` on `` element in the app manifest. – rubo Apr 02 '19 at 20:52
5

The only solution I've found was to set the gravity programatically to LEFT or RIGHT after setting the inputType.

proowl
  • 101
  • 2
  • 7
1

In my case, the problem was simply solved by changing the layout_width to wrap_content.

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
1

If you put inputType = textPassword or set a passwordTransformation method on EditText, text direction is taken as LTR. It means RTL for password is discouraged. You need to write custom TextView to override this behaviour.

Code snippet from android source for TextView.

// PasswordTransformationMethod always have LTR text direction heuristics returned by
        // getTextDirectionHeuristic, needs reset
        mTextDir = getTextDirectionHeuristic();


protected TextDirectionHeuristic getTextDirectionHeuristic() {
        if (hasPasswordTransformationMethod()) {
            // passwords fields should be LTR
            return TextDirectionHeuristics.LTR;
        }
PK Gupta
  • 822
  • 10
  • 20
1

In My Case both worked fine.

1) android:textAlignment="viewStart"

And

2)

https://stackoverflow.com/a/38291472/6493661

ajay singh
  • 169
  • 1
  • 5
0

and the right answer is:

RtlEditText mUserPassword = root.findViewById(R.id.register_fragment_password);
mUserPassword.setTransformationMethod(new AsteriskPasswordTransformationMethod());

creating our own EditText!

it work prefectly only if you replace the dot with astrix by AsteriskPasswordTransformationMethod below this code.

public class RtlEditText extends EditText {


public RtlEditText(Context context) {
    super(context);
}

public RtlEditText(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public RtlEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public RtlEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public TextDirectionHeuristic getTextDirectionHeuristic() {
        // passwords fields should be LTR
        return TextDirectionHeuristics.ANYRTL_LTR;
}}



public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
    return new PasswordCharSequence(source);
}

private class PasswordCharSequence implements CharSequence {
    private CharSequence mSource;
    public PasswordCharSequence(CharSequence source) {
        mSource = source; // Store char sequence
    }
    public char charAt(int index) {
        return '*'; // This is the important part
    }
    public int length() {
        return mSource.length(); // Return default
    }
    public CharSequence subSequence(int start, int end) {
        return mSource.subSequence(start, end); // Return default
    }
}

}