4

When I copy an email from a website and paste it in my edit text, it comes with the rich formatting and then I tried to remove the formatting

public void afterTextChanged(Editable string)
{
    CharacterStyle[] toBeRemovedSpans = string.getSpans(0, string.length(),
                                                MetricAffectingSpan.class);
    for (int index = 0; index < toBeRemovedSpans; index++)
        string.removeSpan(toBeRemovedSpans[index]);
    }
}

This image is before formatting

but it didn't worked for me like their is a huge gap from starting to the text.This image is after applying the formatting

I have gone through whole internet to stop it but didn't find anything. I tried clearing all spans from edit text but it made edit text a way slow than before.

<LinearLayout
                    android:id="@+id/email_signup_form"
                    android:layout_width="match_parent"
                    android:layout_marginTop="20dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/SIZE_50"
                        android:paddingLeft="@dimen/activity_horizontal_margin"
                        android:paddingRight="@dimen/activity_horizontal_margin"
                        app:hintTextAppearance="@style/MyAppearance">

                        <EditText
                            android:id="@+id/first_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/prompt_fname"
                            android:inputType="textPersonName"
                            android:maxLength="64"
                            android:maxLines="1"
                            android:singleLine="true"
                            />

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

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingLeft="@dimen/activity_horizontal_margin"
                        android:paddingRight="@dimen/activity_horizontal_margin"
                        app:hintTextAppearance="@style/MyAppearance">

                        <EditText
                            android:id="@+id/last_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/prompt_lname"
                            android:inputType="textPersonName"
                            android:maxLines="1"
                            android:maxLength="64"
                            android:singleLine="true" />

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

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:paddingLeft="@dimen/activity_horizontal_margin"
                        android:paddingRight="@dimen/activity_horizontal_margin"
                        android:layout_height="wrap_content"
                        app:hintTextAppearance="@style/MyAppearance">

                        <EditText
                            android:id="@+id/email"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/prompt_email"
                            android:inputType="textEmailAddress"
                            android:maxLines="1"
                            android:singleLine="true" />

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

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/passwordText"
                        android:paddingLeft="@dimen/activity_horizontal_margin"
                        android:paddingRight="@dimen/activity_horizontal_margin"
                        app:passwordToggleEnabled="true"
                        app:hintTextAppearance="@style/MyAppearance">

                        <EditText
                            android:id="@+id/password"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/prompt_password"
                            android:inputType="textPassword"
                            android:maxLines="1"
                            android:singleLine="true" />

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

                    <com.google.android.material.textfield.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/retypePasswordText"
                        android:paddingLeft="@dimen/activity_horizontal_margin"
                        android:paddingRight="@dimen/activity_horizontal_margin"
                        app:passwordToggleEnabled="true"
                        app:hintTextAppearance="@style/MyAppearance">

                        <EditText
                            android:id="@+id/cnf_password"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/prompt_password_cnf"
                            android:inputType="textPassword"
                            android:maxLines="1"
                            android:singleLine="true" />

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

                    <Button
                        android:id="@+id/email_sign_up_button"
                        style="?android:textAppearanceSmall"
                        android:layout_width="265dp"
                        android:layout_gravity="center"
                        android:background="@drawable/ripple_effect"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:textColor="#fff"
                        android:text="@string/action_sign_up"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center_horizontal|bottom">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:src="@drawable/characters"/>
                    </LinearLayout>


                </LinearLayout>

this problem is for every edit text. Thing is when I add text before pasting the text, edit text works without any gap or anything after text is pasted it works weird

  • probably this can be due to input type. like you are copying the text and pasting and expecting as textEmailAddress. Have you tried removing inputType any way ?? – Sanket Vekariya Apr 18 '20 at 16:04
  • Have you checked? -> https://stackoverflow.com/questions/24758698/paste-without-rich-text-formatting-into-edittext – Sanket Vekariya Apr 18 '20 at 16:06

2 Answers2

-1

Can you post the code of your Layout please ? I think the text alignment of the EditText is "centered", you juste have to remove it.

Bubu
  • 1,533
  • 14
  • 14
  • I don't think it is about the alignment. It's like more about the spans already added in the text which we have to remove – Anyone else Apr 17 '20 at 16:46
  • 1
    I think you should probably use a comment when asking for clarification. Your Answer doesn't appear to be an answer. It's likely to be removed or downvoted. – Scratte Apr 17 '20 at 18:11
-1

Please remove below code from TextInputLayout..

android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
Jaimil Patel
  • 1,301
  • 6
  • 13