I am creating an application which has a \'To\' field just like in Facebook app\'s \"New Message\" feature.
After selecting an item from the drop down list, I create
Editable buffer = s;
int start = multiContentText.getSelectionStart()<0?0:multiContentText.getSelectionStart();
int end = multiContentText.getSelectionEnd()<0?0:multiContentText.getSelectionEnd();
if(noOfCharAdded==0 && noOfCharDeleted==1){ //if space is deleted
if (start == end && delPrevText) {
ImageSpan link[] = buffer.getSpans(start, end,ImageSpan.class);
if (link.length > 0) {
for(int i=0;i<contentArray.size();i++){
JSONObject jo=contentArray.get(i);
try {
int keyValue=jo.getInt("startIndx");//No i18N
if(keyValue==buffer.getSpanStart(link[0])){
jo.put("isRemoved", true);
contentArray.set(i,jo);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
buffer.replace(buffer.getSpanStart(link[0]),buffer.getSpanEnd(link[0]),"");
buffer.removeSpan(link[0]);
}
}
delPrevText=true;
multiContentText.setSelection(multiContentText.getText().length());
}
else if(noOfCharAdded==0 && noOfCharDeleted>1){//if the whole word is deleted
if(buffer.length()>0){
if(start<buffer.length()){
delPrevText=false;
for(int i=0;i<contentArray.size();i++){
JSONObject jo=contentArray.get(i);
try {
int keyValue=jo.getInt("startIndx");//No i18N
if(keyValue==start){
jo.put("isRemoved", true);
contentArray.set(i,jo);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(buffer.charAt(start)==' '){
buffer.replace(start,start+1,"");
}
}
}
}
Try adding a TextWatcher
to the MultiAutoCompleteTextView
.
Save the current text and check if the last space was deleted.
If so, remove the last token.
Found the solution....
Add this textwatcher to the multiautocompletetextview
private TextWatcher textWather = new TextWatcher() {
int noOfCharAdded=0;int noOfCharDeleted=0;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
startIdx=start;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
noOfCharAdded=after;
noOfCharDeleted=count;
}
@Override
public void afterTextChanged(Editable s) {
Editable buffer = s;
int start = multiContentText.getSelectionStart()<0?0:multiContentText.getSelectionStart();
int end = multiContentText.getSelectionEnd()<0?0:multiContentText.getSelectionEnd();
if(noOfCharAdded==0 && noOfCharDeleted==1){ //if space is deleted
if (start == end && delPrevText) {
ImageSpan link[] = buffer.getSpans(start, end,ImageSpan.class);
if (link.length > 0) {
buffer.replace(buffer.getSpanStart(link[0]),buffer.getSpanEnd(link[0]),"");
buffer.removeSpan(link[0]);
}
}
delPrevText=true;
multiContentText.setSelection(multiContentText.getText().length());
}
else if(noOfCharAdded==0 && noOfCharDeleted>1){//if the whole word is deleted
if(buffer.length()>0){
if(start<buffer.length()){
delPrevText=false;
if(buffer.charAt(start)==' '){
buffer.replace(start,start+1,"");
}
}
}
}
}
};