
I am trying to align the TextView around ImageView. I am using the following code:
private void createSpannableText(){
TextView myTextView = (TextView) findViewById(R.id.textView);
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(this.getText(R.string.loren__ipsum__max));
int lengthOfPart1 = builder.length();
builder.append(" ");
builder.append(this.getText(R.string.lorem__ipsum));
Drawable d = getResources().getDrawable(R.drawable.myImage);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); // <---- Very important otherwise your image won't appear
ImageSpan myImage = new ImageSpan(d);
builder.setSpan(myImage, 0, lengthOfPart1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(builder);
}
But couldn’t get the exact result. What shall I do? Do I need to use SpannableStringBuilder in this case or there is another way. Please help. I used this post-http://majaxandroidtips.blogspot.in/2009/06/how-to-have-few-layout-elements-wrap_17.html
to have the solution.
P.S.: I also want 6dp margin around the ImageView
You can achieve this by using the
android.text.style.LeadingMarginSpan.LeadingMarginSpan2interface which is available in API 8. Here is the article, not in English though, translate it using your browser. Besides you can download the source code of the example directly from here.Your layout:
Helper class implements
LeadingMarginSpan.LeadingMarginSpan2Your activity code:
And finally here a demo result: