Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android cursor in RTL languages

I have problem with android input cursor in RTL languages. When I am in RTL support layout, I have two input cursor and it's really funny. Does it have real solution, for get rid of this?

I use this code to make my android UI RTL:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

And this my xml for text view:

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:id="@+id/myID"
            android:maxLines="1"
            android:focusableInTouchMode="true"
            android:layout_weight="0.25"
            android:layout_gravity="center_horizontal"
            android:hint="phone Number"
            android:maxLength="20"
            android:gravity="center" />
    </android.support.design.widget.TextInputLayout>

enter image description here

like image 531
Amir H Avatar asked Feb 13 '16 09:02

Amir H


People also ask

How to set RTL in android?

In your android phone, tap on “Settings” icon. Now tap on developer options and search for “Force RTL layout direction”. Tap on it to enable RTL feature.

How do you change the color of your cursor on android?

Setting the android:textCursorDrawable attribute to @null should result in the use of android:textColor as the cursor color. oh man that is SO much more efficient than conjuring up a drawable for the cursor to be black!!


2 Answers

I solve the problem. Just make the edit text language left to right and problem is solved.

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/PhoneNoET"
    android:layout_marginTop="64dp"
    android:layout_below="@+id/textView6"
    android:layout_centerHorizontal="true"
    android:textAlignment="center"
    android:maxLength="11" 
    android:layoutDirection="ltr"/>

Add the above layoutDirection to your Edit Text and fix the problem.

like image 136
Amir H Avatar answered Oct 03 '22 13:10

Amir H


I resolved it using below..

In your xml layout, for edit text field add this-->

android:textDirection="anyRtl"

Here, "editText" is your view of edit text field. i.e,

EditText editText = (EditText) findViewById(R.id.edit_text); 

Then programatically add text watcher for that edit text.

editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    editText.setSelection(s.length()); // this line is very important for setting cursor position
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }

This worked for me!

like image 22
Arjun G Avatar answered Oct 03 '22 12:10

Arjun G