0

does anyone have an idea why the following code does not change the data? The "expired" string is added, but neither the font color nor the size is changed.

        Spannable expired = new SpannableString(" expired");
        expired.setSpan(new ForegroundColorSpan(Color.RED), 0, expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        expired.setSpan(new RelativeSizeSpan(.5f), 0, expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        nameTextView.setText(goalsName + expired);

xml:

        <TextView
        android:id="@+id/goal_list_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="22sp"
        android:layout_marginBottom="8sp"
        android:focusable="true"
        tools:text="Cel 1" />
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kamil
  • 113
  • 1
  • 10
  • 2
    You have to take full string then change(manipulate) the string on basis of index and set the full spannable to string instead of concatenating two different one. – Surender Kumar Dec 06 '18 at 11:47
  • 1
    By concatenating your Spannable to a String you are converting it to another String, removing the styling information. – RobCo Dec 07 '18 at 10:39

2 Answers2

1

The correct solution:

String s = name + " expired";
SpannableString  expired = new SpannableString(s);
expired.setSpan(new ForegroundColorSpan(Color.RED), name.length(), expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
expired.setSpan(new RelativeSizeSpan(.7f), name.length(), expired.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
nameTextView.setText(expired);
Kamil
  • 113
  • 1
  • 10
0

You have used Spannable, You should be using SpannableString instead. Change your code as below.

String s = "oldString"+" expired";
SpannableString expired=  new SpannableString(s);
expired.setSpan(new ForegroundColorSpan(Color.RED), 10, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
expired.setSpan(new RelativeSizeSpan(.5f), 10, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
nameTextView.setText(expired);
karan
  • 8,637
  • 3
  • 41
  • 78