12

I want to show the "♫" character in an Android TextView, but it shows [] instead.

This is my code:

txtCatname.setText("♫");

How can I display this symbol correctly?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Alireza
  • 209
  • 2
  • 3
  • 10

6 Answers6

30

You can use an Unicode code: http://unicode-table.com/en/.

Such as:

txtCatname.setText("\u266b");

or alternatively use an iconic font, such as font awesome:
http://fortawesome.github.io/Font-Awesome/

Use this alternative (or any other iconic font you like), in case this character isn't supported (not all Unicode characters are supported).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
4

Try this:

String str = "♫";
byte spbyte[] = str.getBytes("UTF-8"); 
str = new String( spbyte,"UTF-8");
txtCatname.setText(str);

If it doesn't work, try this:

String str = "♫";
txtCatname.setText(Html.fromHtml(str));
Pang
  • 9,564
  • 146
  • 81
  • 122
usr30911
  • 2,731
  • 7
  • 26
  • 56
  • 1
    .setText(Html.fromHtml(str)); This part makes your answer the best one. At least for my case! – Andris Feb 11 '19 at 10:56
2

If nothing else helps, you can either:

  1. use a custom font (e.g. http://www.tutorialspoint.com/android/android_custom_fonts.htm or Add custom font for complete android application ) or

  2. add an image: How to add image in a TextView text?

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
1

That is common when the source file is encoded as ANSI. Converting the source file as UTF-8 (without BOM) will likely solve the issue.

MarcoP
  • 190
  • 1
  • 6
1

This seems pretty simple now:

  • Simply go to unicode list and choose your unicode and copy go it.

  • Go to strings.xml and paste it. enter image description here This will paste the unicode character instead of it's code and use it in your xml as a normal string.

Hope this will help you.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
0

Simply append the symbol as a string in your string.xml By adding double quotes to the special character.

ARMEL FOPA
  • 206
  • 3
  • 8