Setting an indispensable string not working on plain Textview

I can’t understand for life why this simple code, to set a spannable string, does not work on this textview. In the method below, the Today marker is added, which should be green, before the text displaying the date if the date is the current day.

private void setTimeTextView(String timeString) { Calendar c = Calendar.getInstance(); String todaysDateString = ApiContentFormattingUtil.getFullDateFormat(c.getTime()); if (timeString.equals(todaysDateString)){ String todayText = getResources().getString(R.string.today_marker); Spannable timeSpannable = new SpannableString(todayText + timeString); timeSpannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.greenish_teal)), 0, todayText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mDateTime.setText(timeSpannable); } else { mDateTime.setText(timeString); } } 

However, the color will not change.

enter image description here

Here is the XML for this view

 <TextView android:id="@+id/newsfeed_date_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="23dp" android:textSize="12sp" android:textColor="@color/white_three" android:letterSpacing="0.06" app:fontPath="@string/opensans_bold_path" tools:text="Monday, January 1st" android:textAllCaps="true" tools:ignore="MissingPrefix" tools:targetApi="lollipop"/> 
+5
source share
1 answer

The textAllCaps attribute robs any Spannable information on your String . You need to remove this (or set it to false ) and handle the conversion to uppercase yourself before creating a SpannableString from it. For instance:

 String todayText = getResources().getString(R.string.today_marker); String text = todayText + timeString; Spannable timeSpannable = new SpannableString(text.toUpperCase()); 

This is a known bug with the textAllCaps attribute, in particular with AllCapsTransformationMethod .

http://code.google.com/p/android/issues/detail?id=67509

+21
source

Source: https://habr.com/ru/post/1260846/


All Articles