j2emanue
j2emanue

Reputation: 62519

Android custom TextInputEditText - cursor constantly staying at start position

I have a custom TextInputEditText which I am using as a password field. However, the cursor will not stay in position and always goes back to the start of the edittext.

The custom view looks like this:

public class CustomInputEditTextWithSecret extends TextInputEditText {
    private final String SECRET = "123456";

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

    public CustomInputEditTextWithSecret(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomInputEditTextWithSecret(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public Editable getText() {
        Editable s = (Editable) super.getText();
        if(s!=null && s.length()>0) {
            return new SpannableStringBuilder(SECRET+s);
        }
         return s;
    }
}

When I use it in XML layout it looks like this:

<android.support.design.widget.TextInputLayout
            android:id="@+id/password_layout"
            style="@style/FirebaseUI.TextInputLayout.PasswordField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/fui_field_padding_vert"
            app:layout_constraintTop_toBottomOf="@+id/welcome_back_password_body"
            app:passwordToggleEnabled="true">

            <com.firebase.ui.auth.ui.customviews.CustomInputEditTextWithSecret
            android:layout_width="match_parent"
              android:layout_height="wrap_content"
                android:id="@+id/password"
        />

             </android.support.design.widget.TextInputLayout>



             if you need the entire layout here it is:

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

    <android.support.constraint.ConstraintLayout
        style="@style/FirebaseUI.WrapperStyle"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/heading"
            style="@style/FirebaseUI.Text.Heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/fui_welcome_back_email_header"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/welcome_back_password_body"
            style="@style/FirebaseUI.Text.BodyText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="false"
            app:layout_constraintTop_toBottomOf="@+id/heading"
            tools:text="@string/fui_welcome_back_password_prompt_body" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/password_layout"
            style="@style/FirebaseUI.TextInputLayout.PasswordField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/fui_field_padding_vert"
            app:layout_constraintTop_toBottomOf="@+id/welcome_back_password_body"
            app:passwordToggleEnabled="true">

            <com.firebase.ui.auth.ui.customviews.CustomInputEditTextWithSecret
            android:layout_width="match_parent"
              android:layout_height="wrap_content"
                android:id="@+id/password"
        />

        </android.support.design.widget.TextInputLayout>

        <TextView
            android:id="@+id/trouble_signing_in"
            style="@style/FirebaseUI.Text.Link"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/fui_field_padding_vert"
            android:text="@string/fui_trouble_signing_in"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button_done"
            app:layout_constraintTop_toBottomOf="@+id/password_layout" />

        <Button
            android:id="@+id/button_done"
            style="@style/FirebaseUI.Button"
            android:text="@string/fui_sign_in_default"
            app:layout_constraintStart_toEndOf="@+id/trouble_signing_in"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/trouble_signing_in" />

    </android.support.constraint.ConstraintLayout>

</ScrollView>

Is it some security feature on the password field? What am I doing wrong? In the custom view what I am trying to do is attach a string to the end of every password.

Here is a demo video of what is happening (the video is called demo.mp4).

UPDATE: IF I COMMENT out the getText() override then it works fine. so i must be doing something wrong there. how can i attach a string everytime the developer calls getText() ?

Upvotes: 1

Views: 922

Answers (1)

Bradleycorn
Bradleycorn

Reputation: 1198

I had this problem with the standard TextInputEditText. Explicitly adding android:inputType="text" fixed it for me:

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_username_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/login_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionNext"
            android:inputType="text" /> <!-- explicit inputType fixes it for me -->

    </com.google.android.material.textfield.TextInputLayout>

Looks like maybe you are using a password field .. setting android:intputType="textPassword" also works for me.

Upvotes: 2

Related Questions