Sometimes, when pasting text in an EditText, blanks/spaces are automatically inserted. For example, this happens if text is pasted into the middle or at the end of text that is already contained in the text field. Is there a way to tell the EditText object or the ClipboardManager that leading and trailing blanks should not automatically be inserted?
-
1i can't confirm this behavior. maybe it's your keyboard? have you tried the android keyboard instead? – android developer Apr 25 '13 at 17:13
-
1You could just remove them in the code like here: http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android – Apr 25 '13 at 17:20
4 Answers
use trim(); it will remove blanks from before and after a string
str = str.trim();

- 6,824
- 8
- 40
- 59
-
1I have no idea where to add the call of trim(). When I retrieve the content of the clipboard via the ClipboardManager, no blanks can be found. I also tried an InputFilter but here the filter method is called once for a single leading blank, once for a trailing blank and once for the actual clipboard content. In the InputFilter I cannot decide whether a single blank comes from the user or this studip paste-method. – EricJobs Apr 29 '13 at 14:21
-
1Seems like I am not the only person botherd by this automatism: http://code.google.com/p/android/issues/detail?id=41037 – EricJobs Apr 29 '13 at 14:32
-
yeah i never even tried this, i read about it and thought it would apply to your question – JRowan Apr 29 '13 at 16:49
I was needed prevent "space" before do work with InputFilter, so I use that:
mSearchText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
MyLog.d(this, keyEvent);
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
switch (keyEvent.getAction()) {
case KeyEvent.ACTION_DOWN:
mSpacePressed = true;
break;
case KeyEvent.ACTION_UP:
mSpacePressed = false;
}
}
return false;
}
});
InputFilter mSearchFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)
{
//Prevent adding spaces on Paste
//Remember: if you want paste " " it will be prevented
if (arg0.toString().equals(" ")){
if (!mSpacePressed){
return "";
}
}
//do work with filter
return null;
}
};
mSearchText.setFilters(new InputFilter[]{mSearchFilter});

- 2,671
- 1
- 13
- 16
I'm late but in my old tablet, my EditText also works like you said.
So, I fixed it like below.
1.Create a class to store the information about the start index and the length of inserted string.
public class PasteUnit {
int start = 0;
int length = 0;
public PasteUnit(int start, int length) {
this.start = start;
this.length = length;
}
}
2.Create a class that extends EditText.
public class MyEditText extends EditText
{
boolean pasteStarted = false;
ArrayList<PasteUnit> pasteUnits = new ArrayList<>();
public MyEditText(Context context)
{
super(context);
}
public MyEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public boolean onTextContextMenuItem(int id) {
if(id==android.R.id.paste) {//This is called when the paste is triggered
pasteStarted = true;
}
boolean consumed = super.onTextContextMenuItem(id);
//the super.onTextContextMenuItem(id) processes the pasted string.
//This is where EditText acts weird.
//And we can watch what is happening in our TextWatcher to be added below
switch (id){
case android.R.id.paste://This is called after we collected all the information
if(pasteUnits.size()>1) {//When a space or spaces are inserted
int startIndex = pasteUnits.get(0).start;
int totalLength = 0;
for(int i=0;i<pasteUnits.size();i++) {
PasteUnit pasteUnit = pasteUnits.get(i);
totalLength = totalLength + pasteUnit.length;
}
int endIndex = startIndex + totalLength;
String string = this.getText().toString();
String before = string.substring(0, startIndex);
String after = string.substring(endIndex);
PasteUnit lastPasteUnit = pasteUnits.get(pasteUnits.size()-1);
String lastString = string.substring(lastPasteUnit.start, lastPasteUnit.start + lastPasteUnit.length);
String result = before + lastString + after;
this.setText(result);
this.setSelection(startIndex + lastString.length());
}
pasteUnits.clear();
pasteStarted = false;
break;
case android.R.id.copy:
break;
case android.R.id.cut:
break;
}
return consumed;
}
}
3.Add TextWatcher to your EditText.
The old EditText acts really weird seeing the TextWatcher. When you paste string A into string B, it first inserts a space into string B, and it inserts another space into string B, and last it inserts string A between the two spaces.
And in other situations like pasting string A after string B, it first appends a space after string B and after that appends string A.
So anyway it seems to insert the original string at the last step.
MyEditText edit = (MyEditText) findViewById(R.id.mainEditText1);
edit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(edit.pasteStarted) {//when it is processing what we pasted
edit.pasteUnits.add(new PasteUnit(start, count));
//store the information about the inserted spaces and the original pasted string
}
}
});

- 1
- 1