Muhammad Abdullah
Muhammad Abdullah

Reputation: 137

How to change textsize of a word in a string Android?

I have a String which should shows my value out of a total value, Like 2/5 ,But what i want to do is show it as 2 or the number before the "/" is of a comparatively larger size then the number after the "/" . This is what i have tried so far, and it has no effect.

    SpannableString ss1=  new SpannableString(String.valueOf(connectedDevices + "/" + totalDevices));
    ss1.setSpan(new RelativeSizeSpan(2f), 0,1, 0); // set size
    mData.get(0).setCurrentCount(String.valueOf(ss1) );
    notifyDataSetChanged();

I want the size of connectedDevices to be N (e.g. N=3) times of the size of totalDevices. How can I format this string?

The difference from the link this question is marked as duplicate is that I am storing this text in a string or in any variable and then using it later on through the adapter of a recyclerview.

Upvotes: 0

Views: 74

Answers (1)

Mike M.
Mike M.

Reputation: 39191

A plain String does not hold any span formatting information, so the String.valueOf(ss1) call is basically undoing the formatting you've done with the SpannableString and the RelativeSizeSpan.

To preserve that formatting, change the type of the relevant field, as well as the parameter types on any corresponding methods, to CharSequence, which is the base interface that pretty much all textual data types implement. You would then pass that SpannableString directly to setCurrentCount(), instead of first converting it to a String.

If, at some point, you do need a flat String, you can convert it then; e.g., with toString() or String.valueOf().

Upvotes: 2

Related Questions