I have multiple paragraphs in one TextView
and I am trying to achieve a kind of "Tabular" effect. The first line has a zero indentation and a tab indent (I use a TabStopSpan
for this) and the rest use a LeadingMarginSpan.Standard(0, INDENTATION)
for the effect.
It looks like this:
As you can see, the indentations are applied to both paragraphs, but the lines break properly, only in the first one, and they go off-screen in the second one. Here is the code I used to implement this:
public class MyActivity extends Activity implements {
DemoTextView demoTextView;
int columnIndentation = 150;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
demoTextView = (DemoTextView) findViewById(R.id.demo_textview);
SpannableStringBuilder demoSpannableString = getDemoSpannable();
demoSpannableString.append("\n\n");
demoSpannableString.append(getDemoSpannable());
demoTextView.setText(demoSpannableString, TextView.BufferType.SPANNABLE);
}
private SpannableStringBuilder getDemoSpannable () {
SpannableStringBuilder demoSpannableString = new SpannableStringBuilder(getDemoText());
demoSpannableString.setSpan(new TabStopSpan() {
@Override
public int getTabStop() {
return columnIndentation;
}
}, 0, demoSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
demoSpannableString.setSpan(new LeadingMarginSpan.Standard(0, columnIndentation), 0, demoSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
demoSpannableString.setSpan(new MyTypefaceSpan(this, TIMES_NEW_ROMAN), 0, demoSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return demoSpannableString;
}
public String getDemoText () {
return "Lorem \tipsum dolor sit amet, consectetur adipiscing elit. Donec lobortis condimentum tincidunt. Suspendisse leo risus, auctor sit amet erat et, ultricies fringilla augue. Integer feugiat elementum turpis, non finibus mi sodales vitae. Proin scelerisque hendrerit orci, in viverra nulla. Curabitur tempor orci non metus semper, ut vehicula massa euismod. Phasellus ultrices ipsum sit amet lobortis imperdiet. Aenean libero nulla, vestibulum id efficitur volutpat, feugiat sagittis nunc.";
}
}
Any idea how I can avoid the text going off-screen in subsequent paragraphs?
It seems that this is a bug affecting all Android versions above 3.0, any LeadingMarginSpan where 'first' and 'rest' aren't the same will suffer from this issue. If you used
LeadingMarginSpan.Standard(columnIndentation)
instead of
LeadingMarginSpan.Standard(0, columnIndentation)
second paragraph will not be trimmed but TabStopSpan will not work.
Check related issue, https://code.google.com/p/android/issues/detail?id=38003