Here is the TextView that I am working with:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:singleLine="true"
android:text="TextView for test only"
android:textColor="#fff" />
</androidx.constraintlayout.widget.ConstraintLayout>
The default text color is white. My goal is to change the color of the first character to black and the background color of the first character to white. This is the code I am currently using:
TextView txt = (TextView) findViewById(R.id.txt);
SpannableStringBuilder sp = new SpannableStringBuilder(txt.getText());
sp.setSpan(new BackgroundColorSpan(Color.WHITE), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
sp.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setText(sp);
Everything functions as expected, with the exception of the background color of the first character. I need it to take up the full height of the TextView, not just the space between the text.
This is the current result:
And this is the desired outcome: (Created using Figma)
I hope this provides a clear understanding of my issue. Thank you for your help.