5

I need to have a text view displaying two texts - one of them left aligned, the other - right aligned. After reading about spans, I composed the following code:

private Spannable generateText(PageModel page) {
    final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);

    final String pageNumber = page.getNumber() + "  ";
    final String string = pageNumber  + dateFormat.format(new Date(page.getLastModified())) ;
    Spannable spannable = Spannable.Factory.getInstance().newSpannable(string);
    spannable.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_NORMAL), 0, pageNumber.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), pageNumber.length() + 1, string.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannable;
}

Spannable spannable = generateText(page);
textView.setText(spannable);

The result is that both are left(normal) aligned. Actually, no alignment happens at all, even if I use center align. The bounds of string parts do not cross over, why does it not work? Do you have any idea why would this not work?

George
  • 3,727
  • 9
  • 31
  • 47
  • 1
    Its too late to add a comment.. It works only when there is a new line feed between left text and right text. Otherwise it won't work. If anyone knows the correct solution please post.. – Ankit Aggarwal Jul 27 '16 at 14:48

0 Answers0