0

I am trying copy a password from Chrome Browser (in Android) into a EditText but it always copy the format, including two space at the end.

In XML file.

<EditText
    android:id="@+id/signin_etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:layout_marginBottom="5dp"
    android:padding="8dp"/>

My password is

RLqGQQa3

but when I call to getText() it return:

 RLqGQQa3  

with a space at start, and two at end.

These occur only when I copy the password from browser

Cristian
  • 514
  • 4
  • 21
  • you can use trim() method to remove unwanted spaces.. – Lokesh Apr 28 '14 at 10:35
  • 2
    possible duplicate of [Paste without rich text formatting into EditText](http://stackoverflow.com/questions/24758698/paste-without-rich-text-formatting-into-edittext) – lenooh Sep 24 '15 at 16:37

3 Answers3

2

Try this..

trim()

To Remove white space characters from the beginning and end of the string.

getText().toString().trim();
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

In your class, do this:

String  signin_etPassword = EditTextinput.getText().toString();

signin_etPassword= signin_etPassword.trim();

This will remove only the spaces at the beginning or end of the String. Middle of the password will remain.

Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
0

Your question is how to paste without format.

This will remove formatting every time you change text, even on paste.

final EditText et = (EditText) findViewById(R.id.yourEditText);
et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        public void afterTextChanged(Editable s) {
            CharacterStyle[] toBeRemovedSpans =
                    s.getSpans(0, s.length(), MetricAffectingSpan.class);

            for (int i = 0; i < toBeRemovedSpans.length; i++)
                s.removeSpan(toBeRemovedSpans[i]);

        }

    });

Adapted from this answer: Paste without rich text formatting into EditText

Community
  • 1
  • 1
lenooh
  • 10,364
  • 5
  • 58
  • 49
  • If this question is a duplicate of the linked question, please flag it as a duplicate instead of leaving an answer to that effect. If it is not a duplicate, please leave a complete answer to this question instead of a link-only answer. – josliber Sep 24 '15 at 16:30
  • josilber: I don't have enough points to mark questions as duplicates, however, I have flagged it as such. Tnx for pointing it out. – lenooh Sep 24 '15 at 16:38
  • Downvote? What for? For providing a way to the answer? – lenooh Sep 24 '15 at 17:01