How to set field between spannable textview?

I am trying to create a spannable textview and show it in an EditText. That way, the user can type something into the EditText, and if the user presses the keyboard enter button, I will convert that text to spannable textview after this user can start typing again and press the keyboard enter button, and then the second spannable textview will create as, show it in edittext, but.

Where am I stuck?

when I create two spannable textview, these two text comments slightly overlap with each other. And I want to set the difference between these two text fields.

I also tried to set the difference between textview using LayoutParam , but not having time.

Here is an image in which the text display overlaps with each other in an EditText.

enter image description here

y spicy hiding under delicious

Here is my code.

txtDishTags.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH
                    || actionId == EditorInfo.IME_ACTION_NEXT
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || actionId == EditorInfo.IME_ACTION_GO) {
                txtDishTags.dismissDropDown();
                  if(txtDishTags.getText().toString().trim().length()>=1){
                isEntered = true;

                String[] separated = tags.split(",");
                tags = separated[separated.length-1];
                if(tags.trim().length()>=1){
                TextView tv = createContactTextView(tags);
                BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
                bd.setBounds(-20, 0, bd.getIntrinsicWidth(),
                        bd.getIntrinsicHeight());
                sb.append(tags + ",");
                sb1 = new SpannableStringBuilder();
                sb1.append(tags + ",");
                sb.setSpan(new ImageSpan(bd),
                        sb.length() - tags.length(), sb.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                sb.setSpan(clickSpan, sb.length() - tags.length(),
                        sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                txtDishTags.setText("");
                txtDishTags.setText(sb);    
                int length = sb.length();
                txtDishTags.setSelection(length, length);
                }
                }
            }
            return false;
        }
    });  

public TextView createContactTextView(String text) {
    //llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 44);
        //llp.setMargins(5, 0, 20, 0);
    TextView tv = new TextView(this);
    tv.setText(text);
    tv.setTextSize(30);     
    Typeface faceBook = Typeface.createFromAsset(getAssets(),
            "fonts/eau_sans_book.otf");
    tv.setTypeface(faceBook);   
    tv.setTextColor(getResources().getColor(R.color.backgroundcolor));
    tv.setBackgroundResource(R.color.textviewbubble);
    //tv.setLayoutParams(llp);
    Resources r = getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            44, r.getDisplayMetrics());
    tv.setHeight(px);
    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);
}  

I tried to set the difference between textview using the following code

 llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 44);
 llp.setMargins(5, 0, 20, 0);
 tv.setLayoutParams(llp);   

I also install LeftPadding for Textview, but it seems that textview does not receive it at first. Even I set the height in the textview, but it seems that the textview does not get the layout parameter at all. how

int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        44, r.getDisplayMetrics());
tv.setHeight(px);

Please give a link or a hint.
thanks in advance

+3
source share
1 answer

There are several issues that I have identified. You need to make these changes, and everything should work.

Step 1) Update Settings setBounds

On the next line, update the parameters setBoundsfrom -20to 0as follows:

BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());

, , - .

2) sb.setSpan

1 , , ImageSpan ( "," () ). , -1:

sb.setSpan(new ImageSpan(bd), sb.length() - tags.length() - 1, sb.length() - 1,
           Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

.

enter image description here

3)

, , , ", " . , " ". contentBetweenTags . :

String contentBetweenTags = ", ";
sb.append(tags + contentBetweenTags);
sb1 = new SpannableStringBuilder();
sb1.append(tags + contentBetweenTags);
sb.setSpan(new ImageSpan(bd), 
           sb.length() - tags.length() - contentBetweenTags.length(), 
           sb.length() - contentBetweenTags.length(), 
           Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

4)

"margin/spacing" , unicode . , /.

unicode \u2002:

enter image description here

BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
String contentBetweenTags = ",\u2002";
sb.append(tags + contentBetweenTags);
sb1 = new SpannableStringBuilder();
sb1.append(tags + contentBetweenTags);
sb.setSpan(new ImageSpan(bd), 
           sb.length() - tags.length() - contentBetweenTags.length(), 
           sb.length() - contentBetweenTags.length(), 
           Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+8

All Articles