10

I am using an Edit Text in my project.

The problem is that whenever I type anything into the text box, it shows up, but the cursor does not move from its starting position at all, regardless of how many characters I type. Also I am not able to move by clicking to any particular character in the text box.

My xml file for containing the edit text is like this

<EditText
        android:id="@+id/xEt"
        android:layout_width="fill_parent"
        android:layout_height="295dp"
        android:layout_alignParentLeft="true"
        android:ems="10"
        android:focusableInTouchMode="true" 
android:focusable="true" >

The xml file is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/xsubLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" >

    </RelativeLayout>

    <EditText
        android:id="@+id/xEt"
        android:layout_width="wrap_content"
        android:layout_height="295dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

</RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:id="@+id/xK1"
        android:layout_height="wrap_content" android:orientation="vertical"
        android:visibility="gone">
        <include android:id="@+id/xKeyBoard" layout="@layout/gf"></include>
        <include android:id="@+id/xtop" layout="@layout/top"></include>
                <include android:id="@+id/x11" layout="@layout/x1"></include>
        <include android:id="@+id/x12" layout="@layout/x3"></include>
        <include android:id="@+id/x13" layout="@layout/x4"></include>
        <include android:id="@+id/x14" layout="@layout/x5"></include>

        <include android:id="@+id/x15" layout="@layout/x6"></include>
        <include android:id="@+id/x16" layout="@layout/x7"></include>
        <include android:id="@+id/x17" layout="@layout/x8"></include>
        <include android:id="@+id/x18" layout="@layout/x9"></include>

        <include android:id="@+id/x19" layout="@layout/x10"></include>
        <include android:id="@+id/x20" layout="@layout/x11"></include>

        <include android:id="@+id/x22" layout="@layout/x13"></include>

        <include android:id="@+id/x23" layout="@layout/x14"></include>
        <include android:id="@+id/x24" layout="@layout/x15"></include>
        <include android:id="@+id/x25" layout="@layout/x16"></include>
        <include android:id="@+id/x26" layout="@layout/x17"></include>

        <include android:id="@+id/x27" layout="@layout/x18"></include>
        <include android:id="@+id/x28" layout="@layout/x19"></include>


    </RelativeLayout>
</RelativeLayout>
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47

6 Answers6

7

In my case, it was caused by using:

android:textAllCaps="true"

in the EditText element of my xml layout.

When I removed it, it worked as expected

Dharman
  • 30,962
  • 25
  • 85
  • 135
gbenroscience
  • 994
  • 2
  • 10
  • 33
4

Are you binding you view to a model? i.e. Do you have an MVVM setup?

If you do, make sure you do not have any cyclical update especially for a two-way binding i.e. if user types a character, you update your model which will in turn update the EditText control and repeat.

So depending on your Editext control properties, each time the model change updates it, it could have the cursor reset to the start position.

If this is the case, a simple equality check between the model value and the EditText text value before you update it from the model should fix the issue.

if (txtEdit.text != modelValue)
{
    txtEdit.text = modelValue;
}
MADol
  • 41
  • 3
2

It may be a bit too late to answer this question. But even i faced the same problem by using custom Keyboard. The problem was solved for me too after adding the following line in the XML file for every edittext

android:imeOptions="flagNoExtractUi"

android:inputType="textNoSuggestions"

Like this

<Editext                
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:paddingLeft="2dp" 
 android:inputType="textNoSuggestions"
 android:imeOptions="flagNoExtractUi"/>

I have seen a lot of people searching for this so i have posted to the one relevant to my problem.

Community
  • 1
  • 1
Shreyash Khole
  • 560
  • 4
  • 10
  • I'm having this exact problem and this didn't work for me. I'm developing my own keyboard IME too, and this text entry field is in the settings menu of the configuration options for the IME – Papyrus Apr 14 '22 at 15:51
1

set these properties to your XML file

  <YourLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:focusable="true" 
     android:focusableInTouchMode="true">

  <EditText
    android:id="@+id/xEt"
    android:layout_width="fill_parent"
    android:layout_height="295dp"
    android:layout_alignParentLeft="true"
    android:ems="10"
    android:clickable="true"
    android:cursorVisible="true"/> 

</YourLayout>

Try this. and put this in your java code.

 EditText et = (EditText)findViewById(R.id.xEt);
 et.setSelection(et.getText().length());
Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
  • No, it is still not working. What do I do? It remains in the starting position – Aakash Anuj Dec 03 '12 at 11:15
  • I am not using the default keyboard.. I am using my own layout...is that creating a problem? – Aakash Anuj Dec 04 '12 at 03:28
  • please post your layout file. – Nirav Tukadiya Dec 04 '12 at 03:51
  • Nirav, I have to write this code et.setSelection(et.getText().length()); everywhere to achieve the result, since it works for just that moment when it is encountered. Also, I am not able to go back and edit the text. That is , once entered, I am not able to edit the characters. What do I do? – Aakash Anuj Dec 04 '12 at 04:36
  • Logcat shows no errors. The code is too long to post here. All I can say is that I am using my own keyboard layout. Does that create a problem, since I am not using default keyboard? – Aakash Anuj Dec 04 '12 at 05:19
  • no it won't create the problem at all because you have did it right.i guess your java code may contains errors. – Nirav Tukadiya Dec 04 '12 at 05:22
1

I am using this workaround for this problem, If characters are in English or digit characters then change EditText gravity to left else in another Language in my case it's in Arabic then will change gravity to left and the cursor will move in it's right way according to each language:

        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() > 0){
                    char c = s.charAt(0);
                     // characters are Enlgish or digits.
                    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || Character.isDigit(c)) {
                        editText.setGravity(Gravity.LEFT);
                    }else {
                        editText.setGravity(Gravity.RIGHT);


                    }
                } 
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
0

I had exactly this issue, and it was when I was developing a custom Android keyboard IME. None of these proposed solutions worked for me (on Android 12 mind you, much later versions than those used in some of these suggestions).

My problem was in fact caused be the issue @MADol has identified above in that each time the state updated it set the cursor back to the home position (start of the field). I am using a text input field of type AppCompatEditText :

var editText: AppCompatEditText

And in my method that is responsible for updating the text value based on state updates and such I just added:

editText.setSelection(editText.length())

This post here is where I found this solution.

Papyrus
  • 232
  • 3
  • 10