Continuing from my previous post, I was able to set multiple elements to the MultiAutoCompleteTextView
but I was not able to wrap those items with custom background and close button as in that link picture.
I was able to do the same with single element but for multiple, ran out of luck.
This is what I tried.
// set text to MultiAutoCompleteTextView
private void setTextSample(String contactName) {
final SpannableStringBuilder sb = new SpannableStringBuilder();
TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.textview, null);
tv.setText(contactName);
BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
sb.append(contactName + ",");
sb.setSpan(new ImageSpan(bd), sb.length()-(contactName.length()+1),
sb.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mMultiAutoCompleteTextView.setText(sb);
}
// wrap text with custom elements
private static Object convertViewToDrawable(View view) {
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(-view.getScrollX(), -view.getScrollY());
view.draw(c);
view.setDrawingCacheEnabled(true);
Bitmap cacheBmp = view.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
view.destroyDrawingCache();
return new BitmapDrawable(viewBmp);
}
Any help is greatly appreciated.
Edit :
If I do
mMultiAutoCompleteTextView.setText(mMultiAutoCompleteTextView.getText().toString()+", "+sb);
I am getting multiple texts but they are not wrapped with custom background. Not getting where I am going wrong.
Edit :
Sample multiple elements would look something like this