Jehad
Jehad

Reputation: 470

SpannableString With Different Languages in the same TextView

Let's say, I have a TextView in a ListView.

The value of the TextView Is Name(in Arabic) + "Shared" +Name(in English).

English as you Know starts from Left to Right But Arabic starts from Right to Left

When the Both Names are written in English its working Fine

but when the first name is in Arabic its will be like this

"Shared"+ Name(in English)+ Name(in Arabic)

I tried AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE)

but its Not Working

Here is my code :

    SpannableString ss = new SpannableString(PostUserName_ + " " + share + " "
                + uname.trim());

        ClickableSpan FirstName = new ClickableSpan() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onFirstName(v, position, 3);
                }
            }

        };

        ClickableSpan LastName = new ClickableSpan() {
            @Override
            public void onClick(View v) {

                if (listener != null) {
                    listener.onLastName(v, position, 3);
                }
            }

        };

        ss.setSpan((new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE)),0, PostUserName_.length() + share.length() + 1+uname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new TextAppearanceSpan(context,
                android.R.style.TextAppearance_Small), PostUserName_.length(),
                PostUserName_.length()+share.length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ss.setSpan(FirstName, 0, PostUserName_.length() ,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(LastName, PostUserName_.length() + share.length() + 2,
                PostUserName_.length() + share.length() + 1+uname.length()  ,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(0.8f), PostUserName_.length() + 1,
                 PostUserName_.length()+share.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ss.setSpan(new ForegroundColorSpan(Color.BLACK), 0, PostUserName_.length() + share.length() + 1+uname.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.PostUserName.setVisibility(View.VISIBLE);
        holder.PostUserName.setText(ss);

Upvotes: 2

Views: 2134

Answers (2)

hmac
hmac

Reputation: 343

with any arabic text it is liable to default to RTL layout, to force LTR you can do this:

eg. a format string for combining three strings with spaces between:

<string name="three_args_with_spaces_ltr">\u202D%1$s %2$s %3$s\u202C</string>

those unicode chars force the LTR behaviour for more info: https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Bidirectional_General_Formatting

Upvotes: 1

NavinRaj Pandey
NavinRaj Pandey

Reputation: 1704

If possible use two TextViews for english and arabic and align them left and right..

Upvotes: 0

Related Questions