Cover text around image in android

enter image description here

I am using a list view to show the image and text that I want to show, as shown above, can anyone suggest me how to wrap text around the image using webview. I am using the following code:

Drawable dIcon = getResources().getDrawable(R.drawable.video_icon); int leftMargin = dIcon.getIntrinsicWidth() + 10; ImageView icon = (ImageView) findViewById(R.id.icon); icon.setBackgroundDrawable(dIcon); SpannableString ss = new SpannableString(text); ss.setSpan(new MyLeadingMarginSpan2(3, leftMargin), 0, ss.length(), 0); TextView messageView = (TextView) findViewById(R.id.message_view); messageView.setText(ss); 

the class

  class MyLeadingMarginSpan2 implements LeadingMarginSpan2 { private int margin; private int lines; MyLeadingMarginSpan2(int lines, int margin) { this.margin = margin; this.lines = lines; } @Override public int getLeadingMargin(boolean first) { if (first) { return margin; } else { return 0; } } @Override public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout) {} @Override public int getLeadingMarginLineCount() { return lines; } }; 

using this code iam, receiving under the image pls, suggests how to get the first tool, the correct wrapping of text around the image without extra spaces

wrap text around image

+7
android
source share
1 answer

Old post, but since there is no accepted answer, and I just found a solution for the same problem in my application, I will post the solution.

I found that text without line breaks works well. Text with line break that breaks the text into 2 parts so that the part before line break ends to the right of the image, and the part after line break starts already on the next line below the image, this also works well.

So what I do, I set the left edge of the TextView LayoutParams wrapper to the desired indent, and I set the text in the TextView. Then I add OnGlobalLayoutListener and inside the onGlobalLayout callback, I calculate the position of the last character on the last line to the right of the image

 //lines - number of lines to be affected by the leadingMargin int charCount = textView.getLayout().getLineEnd(Math.min(lines - 1, textView.getLayout().getLineCount() - 1)); 

If the text does not contain more lines than the number of lines that the left margin should have (or if the last character is already a line break), I simply set LeadingMarginSpan2 along the entire length of the text.

 // s - original Spannable containing the whole text if (charCount >= s.length() || charCount <= 0 || s.charAt(charCount - 1) == '\n') { s.setSpan(new MyLeadingMarginSpan(lines, w), 0, charCount, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(s); } 

If the text is longer, I split it into two parts (ending at the charCount position first), insert a line break between them, merge them and set LeadingMarginSpan2 only in the first part.

 else { Spannable s1 = new SpannableStringBuilder(s, 0, charCount); s1.setSpan(new MyLeadingMarginSpan(lines, w), 0, charCount, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable s2 = new SpannableStringBuilder(System.getProperty("line.separator")); Spannable s3 = new SpannableStringBuilder(s, charCount, s.length()); textView.setText(TextUtils.concat(s1, s2, s3)); } 

In the end, be sure to remove the left margin of the LayoutParams TextView.

+2
source share

All Articles