0

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.

sourav.bh
  • 467
  • 1
  • 7
  • 20
  • Possible duplicate of [How to increase font size of some part of string in android?](https://stackoverflow.com/questions/22398641/how-to-increase-font-size-of-some-part-of-string-in-android) – Bö macht Blau Dec 18 '18 at 20:00
  • The answer by Raghunandan should be what you're looking for – Bö macht Blau Dec 18 '18 at 20:01
  • The `String.valueOf(ss1)` call is essentially stripping the span you've set on the `SpannableString`. Change your `setCurrentCount()` method, and the underlying field, to handle a `SpannableString`, or something more generic, like `CharSequence`. – Mike M. Dec 18 '18 at 20:02
  • @MikeM. I do get your point but i dont understand how to implement that can you please give me an example ? setCurrentCount() sets a String value and how should i handle a SpannableString with CharSequence ? – Muhammad Abdullah Dec 18 '18 at 20:16
  • 1
    I'm assuming that `mData` is a `List` of some data type that you've written. If so, then you just need to change the current count field in that data class to `CharSequence`, instead of `String`, and similarly for the getter and setter methods' parameters. If, at some point, you do need a flat `String`, you would convert it then; e.g., with `toString()`, or `String.valueOf()`. – Mike M. Dec 18 '18 at 20:20
  • 1
    @MikeM. Perfect. Thankyou so much if you can put that as an answer can mark it as accepted. – Muhammad Abdullah Dec 18 '18 at 20:24

1 Answers1

2

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().

Mike M.
  • 38,532
  • 8
  • 99
  • 95