Reputation: 5913
Below is my Activity
Layout
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:bufferType="spannable"
android:id="@+id/textView"
android:text="@string/empty" />
</RelativeLayout>
and this below is the code which I use within my Activity
TextView tView = (TextView) findViewById(R.id.textView);
for(int i=0;i<15;i++){
appendDrawable(tView,R.drawable.ic_launcher);
appendText(tView,R.string.message);
}
and the methods are :
private void appendDrawable(TextView tView, int drawableId) {
SpannableStringBuilder builder = new SpannableStringBuilder();
String THREE_SPACES = " ";
builder.append(THREE_SPACES);
Drawable drawable = getResources().getDrawable(drawableId);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan image = new ImageSpan(drawable);
builder.setSpan(image, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tView.append(builder);
}
private void appendText(TextView tView, int stringId) {
tView.append(getResources().getString(stringId));
}
and the output which I got is not what I expected 3 missing
and if I tilt the screen 1 missing
Can anyone help me in finding out why the images went missing and if there is a better way of doing this.
Expected Output : As per the loop it should display (drawable, Hello World)15 times but in this example the drawable wasn't rendered thrice(and once when I tilted). Some issues with wrapping?
Upvotes: 0
Views: 1223
Reputation: 1516
builder.setSpan(image, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
should be
builder.setSpan(image, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
since you're using 3 spaces as your span range
Upvotes: 1