How to contact with Bubble

I am trying to create contact bubbles in MultiAutoCompleteTextView , similar to how it is implemented in the Google+ application. Below is a screenshot:

Google+ Compose Post Screenshot .

I tried to extend the DynamicDrawableSpan class to get text stretched against the background of a time lapse

 public class BubbleSpan extends DynamicDrawableSpan { private Context c; public BubbleSpan(Context context) { super(); c = context; } @Override public Drawable getDrawable() { Resources res = c.getResources(); Drawable d = res.getDrawable(R.drawable.oval); d.setBounds(0, 0, 100, 20); return d; } } 

Where my oval.xml output is defined as follows:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#352765"/> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <corners android:radius="6dp" /> </shape> 

In my Activity class, which has a MulitAutoCompleteTextView , I set the bubble range as follows:

 final Editable e = tv.getEditableText(); final SpannableStringBuilder sb = new SpannableStringBuilder(); sb.append("some sample text"); sb.setSpan(new BubbleSpan(getApplicationContext()), 0, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); e.append(sb); 

However, instead of the oval figure being displayed behind the first six characters in the line, the characters are not visible, and there is no oval selection in the background.

If I change the BDBLED getDrawable () method to use .png instead of the form being extracted:

 public Drawable getDrawable() { Resources res = c.getResources(); Drawable d = res.getDrawable(android.R.drawable.bottom_bar); d.setBounds(0, 0, 100, 20); return d; } 

Then .png will appear, but the characters in the string that are part of the range will not be displayed. How can I make it so that the characters in the range are displayed in the foreground, meanwhile, in the background, the displayed custom shape is displayed?

I tried also using ImageSpan instead of a subclass of DynamicDrawableSpan , but failed.

+86
android html android-edittext drawable
May 30 '12 at 8:04
source share
3 answers

Thanks @chrish for the help. So here is how I did it:

 final SpannableStringBuilder sb = new SpannableStringBuilder(); TextView tv = createContactTextView(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); to_input.setText(sb); public TextView createContactTextView(String text){ //creating textview dynamically TextView tv = new TextView(this); tv.setText(text); tv.setTextSize(20); tv.setBackgroundResource(R.drawable.oval); tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear_search_api_holo_light, 0); return tv; } public 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); } 
+51
Jun 02 2018-12-12T00:
source share

Here is the complete solution for you

 //creating textview dynamicalyy TextView textView=new TextView(context); textview.setText("Lauren amos"); textview.setbackgroundResource(r.color.urovalshape); textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_cross, 0); BitmapDrawable dd = (BitmapDrawable) SmsUtil.getDrawableFromTExtView(textView); edittext.settext(addSmily(dd)); //convert image to spannableString public SpannableStringBuilder addSmily(Drawable dd) { dd.setBounds(0, 0, dd.getIntrinsicWidth(),dd.getIntrinsicHeight()); SpannableStringBuilder builder = new SpannableStringBuilder(); builder.append(":-)"); builder.setSpan(new ImageSpan(dd), builder.length() - ":-)".length(),builder.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return builder; } //convert view to drawable public static Object getDrawableFromTExtView(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.getWidth(), view.getHeight(), 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); } 

Here is the complete project file if any of you want to use Spannble

+21
May 31 '12 at 10:04
source share

I have a library that does what you are looking for:

  • Default or fully customizable (you can even use your own layout)
  • Multi-line support
  • Press the listen button

Look at here

Here's a quick start:

Add a ChipView to your layout or create it programmatically:

 <com.plumillonforge.android.chipview.ChipView android:id="@+id/chipview" android:layout_width="match_parent" android:layout_height="wrap_content" /> 

Initiate it with a list of data that extends the abstract Chip and click listener (if you want):

 List<Chip> chipList = new ArrayList<>(); chipList.add(new Tag("Lorem")); chipList.add(new Tag("Ipsum dolor")); chipList.add(new Tag("Sit amet")); chipList.add(new Tag("Consectetur")); chipList.add(new Tag("adipiscing elit")); ChipView chipDefault = (ChipView) findViewById(R.id.chipview); chipDefault.setChipList(chipList); chipDefault.setOnChipClickListener(new OnChipClickListener() { @Override public void onChipClick(Chip chip) { // Action here ! } }); 

By default, ChipView is displayed as follows:

Default chipview

But you can customize as you like, from level to chip level:

Generic ChipView Custom chipview

This is not a MultiAutocomplete, but you can imitate it (I actually use it like that)

+5
Sep 29 '15 at 15:01
source share



All Articles