6

I'm trying to make EditText with a hint text: In English "password" .. the cursor is correctly set to the left. But for Arabic for which the hint is "كلمه المرور" the cursor is always set to the left (the end of the hint) instead of the right.

<EditText
        android:id="@id/ETPass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/etUsrName"
        android:layout_marginLeft="@dimen/_25sdp"
        android:layout_marginRight="@dimen/_25sdp"
        android:layout_marginTop="@dimen/_5sdp"
        android:background="@drawable/signup_edittext_input"
        android:ellipsize="start"
        android:gravity="center|right"
        android:hint="@string/Password"
        android:imeOptions="actionNext"
        android:inputType="textPassword"
        android:paddingRight="@dimen/_5sdp"
        android:singleLine="true"
        android:textColor="@color/orange"
        android:textColorHint="@color/orange" />

This happens only for android:inputType="textPassword" . Everything works fine for a normal text inputType.

samgak
  • 23,944
  • 4
  • 60
  • 82
user3623824
  • 193
  • 3
  • 13

6 Answers6

3

For Android 17 and higher(4.2.+) its working:

android:textAlignment="viewStart"
Ravi Yadav
  • 2,296
  • 3
  • 25
  • 32
1

Try:

android:textDirection="rtl"
Kantesh
  • 333
  • 4
  • 13
0

xml code

   <EditText
                android:maxLength="@integer/lockMaxLen"
                android:inputType="textPassword"
                android:layout_width="180dip"
                android:gravity="right|center"
                android:hint="@string/lock_pass_hint"
                android:layout_height="45dip"
                android:id="@+id/lockPassword" />

in oncreate

 passwordEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence currentDigits, int start,
                                      int before, int count) {
                if (passwordEditText.getText().toString().length() == 0)
                    passwordEditText.setGravity(Gravity.RIGHT);
                else
                    passwordEditText.setGravity(Gravity.LEFT);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
Hassan Karimi
  • 180
  • 3
  • 13
0
Use both, to resolve the alignment issue (even in Samsung device also)

android:textAlignment="viewStart"
android:textDirection="rtl"
omprakash8080
  • 1,211
  • 16
  • 11
0

Maybe I am late, but I faced the same issue. I resolved the issue with the following workaround.

// Get Current language

public static String getCurrentLanguage(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(CURRENT_LANGUAGE, "");
    }

// Check if the language is RTL

public static boolean isRTL(String locale) {
        return TextUtilsCompat.getLayoutDirectionFromLocale(new Locale(locale)) == ViewCompat.LAYOUT_DIRECTION_RTL ? true : false;
    }

// Set gravity to the right

private void repositionPasswordTextInArabic(){
        if( LocaleSettings.isRTL(LocaleSettings.getCurrentLanguage(this))){
            password.setGravity(Gravity.RIGHT);
        }
    }
Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79
0

editText.setSelection(editText.getText().length());

Mosayeb Masoumi
  • 481
  • 4
  • 10