theUturn
theUturn

Reputation: 1131

How to change color of unicode character in TextView?

'from this link of wikipedia I got the code of red heart and want to show in my TextView for generating custom emoji but why is it always showing black insted of red the color of U+2665 is clearly stated red in wikipedia. below is my code

  TextView tv=(TextView)findViewById(R.id.testText);

 //  tv.setText(Html.fromHtml("\u2665"));



tv.setText(Html.fromHtml("<font color='red'>"+"\u2665"+"</font>"));

is not making it red it is still black and it shows red if i put any other text.

Upvotes: 7

Views: 6276

Answers (6)

Sep
Sep

Reputation: 371

Add a

U+FE0E or in this case \uFE0E

VARIATION SELECTOR-15 after each chess piece to force them to render them as text. For reference, U+FE0F VARIATION SELECTOR-16 will force characters to render as emoji.

example :

tv.setText(Html.fromHtml("\u2665\uFE0E"));
tv.setTextColor(Color.BLUE);

Upvotes: 1

Ankit Verma
Ankit Verma

Reputation: 728

Use the code \u2764 for red hearts.

It worked for me.

Upvotes: 0

Michael A. Schaffrath
Michael A. Schaffrath

Reputation: 2110

Explanation: The TextView font renders this as an emoji, which means it basicly uses a predefined image, so font colors are ignored on these. There is a unicode character which can be used as a suffix, to tell the font to use the text variant (VARIATION SELECTOR 15, "\uFE0E", use like this:"\u2665\uFE0E" ), but apparently Android does ignore these, at least on my phone (Samsung SM-G800F with 5.1.1)

The stock browser and chrome also fail in recognizing the variant selector, while Firefox for Android works on my phone.

Apparently this is different accross phones, as you can see in in a related question on Stackexchange UX


Solution: What solved the problem for me, was manually setting another ttf font to the TextView, that supports these characters but not emojis, namely FreeSans from the the GNU FreeFont Project.

Download the font and put it in src/assets/fonts. Then set the font of your TextView:

    Typeface type = Typeface.createFromAsset(getAssets(),"fonts/FreeSans.ttf");
    mTextView.setTypeface(type);

You should propably load the Typeface into a static constant, to avoid recreating it over and over.

Upvotes: 3

Hardik Parmar
Hardik Parmar

Reputation: 712

Also you are set color in xml file in text view like ..

<TextView
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="@dimen/margin_5"
            android:textColor="@color/white"
/>

and in java file to set text custom emoji its work fine.

Upvotes: 0

Husnain Aslam
Husnain Aslam

Reputation: 865

Convert your unicode into Hexadecimal & use it like this. tv.setText(Html.fromHtml("&#x2665;"));

Check my answer for more information.

Upvotes: 0

Zahidul Islam
Zahidul Islam

Reputation: 3190

To make it RED you have to set it's textColor RED. Like this way ->

TextView tv=(TextView)findViewById(R.id.testText);
tv.setText(Html.fromHtml("\u2665"));
tv.setTextColor(Color.RED); // Set color here

Upvotes: 3

Related Questions