Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple alignment in TextView?

I have a TextView like below. I used this code to set gray color for a part of the text.

// Prepare result text.
final String resultText = text + "\n\n" + dictionaryName;
final SpannableString styledResultText = new SpannableString(resultText);
styledResultText.setSpan(new ForegroundColorSpan(Color.GRAY), text.length() + 2, text.length() + 2 + dictionaryName.length(), 0);
resultTextView.setText(styledResultText);

Now I want to set align for it. How to do? Android doesn't have any span class for alignment. I can't find out anything like "AlignmentSpan".

enter image description here

like image 896
emeraldhieu Avatar asked Jul 11 '11 17:07

emeraldhieu


2 Answers

Though its really late to answer, I assume that it might help someone at least. Add this to your code.

styledResultText.setSpan(
    new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), 
    text.length() + 2,
    text.length() + 2 + dictionaryName.length(), 
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);

Alignment.ALIGN_OPPOSITE is the equivalent for right side.

Alignment.ALIGN_NORMAL is the equivalent for left side.

Note: This only works when there is at least 1 new-line character (\n) between the left & right-aligned text.

like image 73
Andro Selva Avatar answered Nov 07 '22 01:11

Andro Selva


I think you should split the text into more than 1 string, put them into separated textview then align one by one.

Hope this helps.

like image 2
Nguyen Minh Binh Avatar answered Nov 07 '22 01:11

Nguyen Minh Binh