I want to change the color of some part of text in EditText. I am trying following code, but it is not working.
SpannableStringBuilder ssb=new SpannableStringBuilder(expression);
ForegroundColorSpan fcs=new ForegroundColorSpan(Color.rgb(181,1,1));
ssb.setSpan(fcs,startIndex,endIndex,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE);
expressionEditText.setText(ssb);
expressionTextView.setText(ssb);
startIndex
andendIndex
are the indices in the expression for which the color needs to be changed.This code works fine for the 'expressionTextView' but not for 'expressionEditText'.
xml part:
<EditText
android:id="@+id/expression_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_above="@id/expression_text_view"
android:gravity="bottom|end"
android:includeFontPadding="false"
android:textSize="45sp"
android:background="@android:color/transparent"
android:inputType="text"/>
<TextView
android:id="@+id/expression_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:gravity="right"
android:textSize="50sp"/>
example:
expression="I want color to be changed";
startIndex=2;
endIndex=5;
In expressionTextView "wan" color will be the specified color. In expressionEditText there will not be any color change.