-1

i want create a TextView and have two text or on text with two color...

i create this ; enter image description here

and i want the sample shop color change to red and search in is blue...

my xml code :

    <TextView
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:background="@drawable/search_bg"
    android:drawableRight="@drawable/search"
    android:drawableTint="@color/day_dark_blue"
    android:paddingRight="15dp"
    android:gravity="center"
    android:layout_gravity="center|center_vertical"
    android:text="search in Sample Shop"
    android:textColor="@color/day_dark_blue"
    android:textSize="18sp" />
MHmansouri
  • 75
  • 11
  • https://stackoverflow.com/questions/6094315/single-textview-with-multiple-colored-text Check if this helps – Sandeep Kumar Feb 02 '21 at 15:46
  • 4
    Does this answer your question? [Single TextView with multiple colored text](https://stackoverflow.com/questions/6094315/single-textview-with-multiple-colored-text) – SebastienRieu Feb 02 '21 at 15:47

2 Answers2

0

You can try using SpannableString

// in the .kt file
val spannable = SpannableString("search in the Sample Shop")
    
spannable.setSpan(
     ForegroundColorSpan(Color.RED), 
     10, 20, 
     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

// 10, 20: start and end index of Sample Shop

myTextView.text = spannable

A helpful article: Spantastic text styling with Spans

vava044
  • 51
  • 8
0

You can use SpannableString to achieve the same.

TextView TV = (TextView)findViewById(R.id.textView);

Spannable wordtoSpan = new SpannableString("search in Sample Shop");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 10, 21, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);
paras jain
  • 41
  • 4