I'm trying to have a TextView
that has two texts, one aligned at the left side of the TextView
and one at the right side of the TextView
.
I referred this [Android TextView Align text to Right and Left (@daemontus) for setting the text.
But how do add a text if I enter the text at right side of the TextView
first and then left side of the TextView
1) Input: Enter Button 1 to display Text1
2) Output: Enter Button 2 to display Text2 along with Text1
public void setLeftRightText(TextView view, String left, String right,Enum keysel) {
if(keysel == RSK_KEY) {
SpannableString merged=new SpannableString(left + "\n" + right);
merged.setSpan(
new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL),
0, left.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE
);
merged.setSpan(
new LineOverlapSpan(),
left.length(), left.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE
);
merged.setSpan(
new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE),
left.length() + 1, left.length() + 1 + right.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE
);
view.setText(merged);
}
else if (keysel == LSK_KEY){
final String resultText = right + " " + left;
final SpannableString styledResultText = new SpannableString(resultText);
styledResultText.setSpan((new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE )), left.length() + 2, left.length() + 2 +right.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
styledResultText.setSpan((new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL )), 0, left.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
view.setText(styledResultText);
}
}
LineOverlapSpan.java
public class LineOverlapSpan implements LineHeightSpan {
public void chooseHeight(final CharSequence text, final int start, final int end, final int spanstartv, final int v, final Paint.FontMetricsInt fm) {
fm.bottom += fm.top;
fm.descent += fm.top;
}
}