2

I got stuck in this problem since yesterday. I am using spannable string and want to add custom shape to it similar to chips:

Here is my code where I am modifying the spannable string:

s.setSpan(new BackgroundColorSpan(backgroundColor), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

I tried with Bitmap but according to process I need to convert my Editable with BitmapDrawable as per the code:

BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(textView);

The problem I am facing is that I am not using textView. I am working on AutoCompleteTextView and converting AutoCompleteTextView to Bitmap is impossible (If I am not wrong here).

How can I achieve the similar result?

Edit-1

I used following shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#A6B0B8" />
<gradient android:startColor="#E5E5E6"
    android:endColor="#B4B6B7"
    android:angle="-270"/>
<corners  android:bottomRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"></corners>
</shape>

Custom Drawable

public class CustomDrawble extends DynamicDrawableSpan {

private Context mContext;

CustomDrawble(Context context){
    super();
    this.mContext = context;
}

@Override
public Drawable getDrawable() {
    Resources res = mContext.getResources();
    Drawable drawable = res.getDrawable(R.drawable.bubble);
    drawable.setBounds(0,0,100,20);
    return drawable;
}
}

For applying above Drawable I used following code:

 s.setSpan(new CustomDrawble(mContext), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

From the #Edit-1 section I am getting this:

enter image description here

What I am doing wrong here? I am using Widget.SearchView not a TextView

Amit Pal
  • 10,604
  • 26
  • 80
  • 160
  • try this http://flavienlaurent.com/blog/2014/01/31/spans/. – Mohammad Hossein Gerami Oct 24 '15 at 04:54
  • @HosseinGerami It's just transforming the text and adding Border to it. I have already done this with `BackgroundColorSpan` (Have a look in the code) – Amit Pal Oct 24 '15 at 05:04
  • ok. or try this http://stackoverflow.com/questions/19161594/android-autocompletetextview-with-chips – Mohammad Hossein Gerami Oct 24 '15 at 05:11
  • @HosseinGerami If I am not wrong then every link is talking about `EditText` or `TextView` But as I have mentioned in question that I am using `Editable` – Amit Pal Oct 24 '15 at 05:20
  • this using autocompletetextview http://wptrafficanalyzer.in/blog/customizing-autocompletetextview-to-display-images-and-text-in-the-suggestion-list-using-simpleadapter-in-android/. ok? – Mohammad Hossein Gerami Oct 24 '15 at 05:27
  • @HosseinGerami No offence but it's completely a different problem and How do you think that it will create bubble to editable ? – Amit Pal Oct 24 '15 at 05:30
  • use `DynameicDrawableSpan` – pskink Oct 24 '15 at 07:23
  • @pskink I followed a SO question and implemented with `DynamicDrawableSpan `. Please have a look at the Edit-1 section – Amit Pal Oct 24 '15 at 07:38
  • it is ok, your Drawables have size 100x20 – pskink Oct 24 '15 at 07:40
  • @pskink can you please provide me reference for `setBound` method. So that I can fix it. BTW I found it but it's not understandable http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setBounds(int, int, int, int) . It sounds like they are talking about co-ordinates – Amit Pal Oct 24 '15 at 07:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93229/discussion-between-amit-pal-and-pskink). – Amit Pal Oct 24 '15 at 07:59

0 Answers0