ninjasense
ninjasense

Reputation: 13856

Can you make an EditText input from right to left?

I was wondering if you can control input on an EditText to move from right to left? The EditText would have to support insert/delete etc. from right to left as well. Is there a way to do this?

Upvotes: 17

Views: 30865

Answers (9)

Huy Tower
Huy Tower

Reputation: 7966

There is the manual way to do it by using the following code :

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // Each time click Menu button on the bar
        // Set the cursor to the first index
        mEtEmail.setSelection(0); // YOUR EDIT TEXT
    } 
    
    return super.onKeyUp(keyCode, event);
}

p/s : Or you can change

KeyEvent.KEYCODE_MENU

Or you can try with Bidi.

Upvotes: 0

Silambarasan Poonguti
Silambarasan Poonguti

Reputation: 9442

Try This....

  1. Initialize User name and password fields.

    EditText username = (EditText) findViewById(R.id.username);
    
    EditText password = (EditText) findViewById(R.id.password);
    
  2. Get current Locale for RTL languages like Arabic etc,.

    String getCurrentLocale = Locale.getDefault().getDisplayLanguage();
    
  3. Then we should check which language is selected for RTL or LTR.

    if(getCurrentLocale.equalEgnoreCase("English")){          //LTR languages
    
          username.setGravity(Gravity.Left);
    
          password.setGravity(Gravity.Left);
    
     }else{                                                     //RTL languages
    
         username.setGravity(Gravity.Right);
    
         password.setGravity(Gravity.Right);
    
     }
    

Upvotes: -2

Sana Ebadi
Sana Ebadi

Reputation: 7220

   <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edit_supporter_last_name"
            style="@style/TextInputEditText"
            android:gravity="right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/ic_person_black_24dp"
            android:inputType="text"
            android:drawablePadding="@dimen/default_margin"
            android:maxLength="10"
            android:padding="@dimen/default_margin" />

OUTPUT :

enter image description here

Upvotes: 0

Ahmed Ashour
Ahmed Ashour

Reputation: 580

The right way is to use:

android:textAlignment="viewStart"

As it will automatically work for LTR and RTL.

Upvotes: 9

Mina Farid
Mina Farid

Reputation: 5887

you can add to your edittext in xml:

in case of left to right :

   android:textDirection="firstStrongLtr"                    

in case of right to left :

   android:textDirection="firstStrongRtl"                    

Upvotes: 0

Fedir Tsapana
Fedir Tsapana

Reputation: 1352

Just use this:

android:layout_gravity="start"
android:textAlignment="viewStart"

Upvotes: 15

M. Usman Khan
M. Usman Khan

Reputation: 4408

Add this to your editText

        android:textDirection="rtl"

Upvotes: 30

Silambarasan
Silambarasan

Reputation: 21

try this code

//initialize
EditText userName = (EditText)findViewById(R.id.userName);

//set gravity for userName 
userName.setGravity(Gravity.RIGHT);

Upvotes: 2

Rich
Rich

Reputation: 36806

It sounds like you just need to set gravity to the right.

Upvotes: 17

Related Questions