How to align TextView around an ImageView
We can achieve this by using the android.text.style.LeadingMarginSpan.LeadingMarginSpan2 interface which is available in API 8.
An extended version of LeadingMarginSpan, which allows the implementor to specify the number of lines of the paragraph to which this object is attached that the "first line of paragraph" margin width will be applied to.
There should only be one LeadingMarginSpan2 per paragraph. The leading margin line count affects all LeadingMarginSpans in the paragraph, adjusting the number of lines to which the first line margin is applied.
As with LeadingMarginSpans, LeadingMarginSpan2s should be attached from the beginning to the end of a paragraph.
Reference ::
http://developer.android.com/reference/android/text/style/LeadingMarginSpan.LeadingMarginSpan2.html
Create Custom Leading Margin
class CustomLeadingMargin implements LeadingMarginSpan2 {
private int marginSpace;
private int numberOfLines;
CustomLeadingMargin(int numberOfLines, int marginSpace) {
this.numberOfLines = numberOfLines;
this.marginSpace = marginSpace;
}
@Override
public int getLeadingMargin(boolean firstLineFlag) {
return firstLineFlag ? marginSpace : 0;
}
@Override
public int getLeadingMarginLineCount() {
return numberOfLines;
}
@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) {}
}
Apply in the TextView
//Get the string from the String File
String descText = getString(R.string.description);
Drawable dIcon = getResources().getDrawable(R.drawable.icon);
int leftMargin = dIcon.getIntrinsicWidth() + 5;
ImageView icon = (ImageView) findViewById(R.id.startImage);
icon.setBackgroundDrawable(dIcon);
SpannableString mSpannableString = new SpannableString(descText);
ss.setSpan(new CustomLeadingMargin(3, leftMargin), 0, mSpannableString.length(), 0);
((TextView) findViewById(R.id.message_view)).setText(mSpannableString);
Full Implementation
Output
We can achieve this by using the android.text.style.LeadingMarginSpan.LeadingMarginSpan2 interface which is available in API 8.
An extended version of LeadingMarginSpan, which allows the implementor to specify the number of lines of the paragraph to which this object is attached that the "first line of paragraph" margin width will be applied to.
There should only be one LeadingMarginSpan2 per paragraph. The leading margin line count affects all LeadingMarginSpans in the paragraph, adjusting the number of lines to which the first line margin is applied.
As with LeadingMarginSpans, LeadingMarginSpan2s should be attached from the beginning to the end of a paragraph.
Reference ::
http://developer.android.com/reference/android/text/style/LeadingMarginSpan.LeadingMarginSpan2.html
Create Custom Leading Margin
class CustomLeadingMargin implements LeadingMarginSpan2 {
private int marginSpace;
private int numberOfLines;
CustomLeadingMargin(int numberOfLines, int marginSpace) {
this.numberOfLines = numberOfLines;
this.marginSpace = marginSpace;
}
@Override
public int getLeadingMargin(boolean firstLineFlag) {
return firstLineFlag ? marginSpace : 0;
}
@Override
public int getLeadingMarginLineCount() {
return numberOfLines;
}
@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) {}
}
Apply in the TextView
//Get the string from the String File
String descText = getString(R.string.description);
Drawable dIcon = getResources().getDrawable(R.drawable.icon);
int leftMargin = dIcon.getIntrinsicWidth() + 5;
ImageView icon = (ImageView) findViewById(R.id.startImage);
icon.setBackgroundDrawable(dIcon);
SpannableString mSpannableString = new SpannableString(descText);
ss.setSpan(new CustomLeadingMargin(3, leftMargin), 0, mSpannableString.length(), 0);
((TextView) findViewById(R.id.message_view)).setText(mSpannableString);
Full Implementation
public class LeadingMarginSampleActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leading_margin_sample);
//Get the string from the String File String descText = "An extended version of LeadingMarginSpan, which allows the implementor to specify the number of lines of the paragraph to which this object is attached that the \"first line of paragraph\" margin width will be applied to.";
Drawable dIcon = getResources().getDrawable(R.drawable.ic_launcher);
int leftMargin = dIcon.getIntrinsicWidth() + 5;
ImageView icon = (ImageView) findViewById(R.id.startImage);
icon.setBackgroundDrawable(dIcon);
SpannableString mSpannableString = new SpannableString(descText);
mSpannableString.setSpan(new CustomLeadingMargin(3, leftMargin), 0, mSpannableString.length(), 0);
((TextView) findViewById(R.id.description)).setText(mSpannableString);
}
class CustomLeadingMargin implements LeadingMarginSpan.LeadingMarginSpan2 {
private int numberOfLines;
private int marginSpace;
CustomLeadingMargin(int numberOfLines, int marginSpace) {
this.numberOfLines = numberOfLines;
this.marginSpace = marginSpace;
}
@Override public int getLeadingMargin(boolean firstLineFlag) {
return firstLineFlag ? marginSpace : 0;
}
@Override public int getLeadingMarginLineCount() {
return numberOfLines;
}
@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) {
}
}
}
activity_leading_margin_sample.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <TextView android:id="@+id/description" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/descText" /> <ImageView android:id="@+id/startImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </RelativeLayout>
Output
30 Hidden Secret Codes for Android Users http://topcellsmartphone.com/30-secret-codes-for-android-users/
ReplyDeleteStackable USB Drive With Virtually Unlimited Memory
DeleteIt would seem like flash drives are old news by now. You put more and more memory into them and increase the price, and that’s about it, right?
http://geekonjava.blogspot.com/2016/01/stackable-usb-drive-with-virtually.html
Also, might I suggest using the RelativeLayout inside a ScrollView, such that it can accommodate large amounts of text inside the TextView.
ReplyDeleteAndroid app development company
Hi,
ReplyDeleteThank you so much for taking effort to share such a useful information. I will definitely share my online portal Android App Development .
Android App Development
Aapthi Technologies
Web Development Company
Thanks for sharing the great post.
ReplyDeleteAndroid Application Development Houston
Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays. Well written article Thank You for Sharing with Us project management courses in chennai | pmp training class in chennai | pmp training fee | project management training certification | project management training in chennai
ReplyDeleteThanks for this amazing post its help me a lot to solve my issues click here to download Kinemaster Mod APK
ReplyDelete[Forwarded from Saeid Saberi]
Deletethere is the best artcile download telegram app
thanks you
[Forwarded from Saeid Saberi]
Deletethere is the best artcile download telegram app
thanks you
https://indiancybersecuritycourse.blogspot.com/2019/07/FilmoraGo-Pro-Mod-Apk-V3.2.0-For-Android.html
ReplyDeleteRecently I am facing this issue, by reading your article now I am able to resolve this bug in my program. Thank for sharing it with us!!
ReplyDeleteAngularjs development company
[Forwarded from Saeid Saberi]
Deletethere is the best artcile download telegram app
thanks you
I’m really impressed with your article, such great & usefull knowledge you mentioned herekinemaster pro mod apk
ReplyDeleteGet your free collection of basic websites templates only in a few clicks. Visit our website for download it.
ReplyDelete[Forwarded from Saeid Saberi]
Deletethere is the best artcile download telegram app
thanks you
[Forwarded from Saeid Saberi]
ReplyDeletethere is the best artcile download telegram app
thanks you
Don’t know how it happened but somehow I managed to meet a soulmate here. Sofia Date women are very versatile and positive interlocutors. Seems there is no topic they can’t talk about, and I like this. Slavic girls are perfect for men looking for a smart friend or bride. jewish dating
ReplyDeleteHi.
ReplyDeleteI understand that you had been searching for such content for a while, and I'm delighted that you finally discovered it on my blog. Here is sharing some Adobe Target Training information may be its helpful to you.
Adobe Target Training