Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode characters not displayed in TextView.setText

Tags:

android

I can't get a TextView to correctly dynamically display unicode characters, and it's driving me batty. I've stripped it down to the bare minimum, but the TextView populated by setText still shows diamonds with question marks inside them for the unicode characters. The version populated from strings.xml shows the multibyte characters perfectly. Here's the activity:

public class TestMultibyteActivity extends Activity {   /** Called when the activity is first created. */   @Override   public void onCreate( Bundle savedInstanceState )   {     super.onCreate( savedInstanceState );     setContentView( R.layout.main );     TextView textField = (TextView) findViewById( R.id.text_field );     String str = "Tübingen systemportefølje";     Log.d( "MULTIBYTE", str ); //note that this prints the multibyte chars correctly.     //EDIT: oh crap, no it doesn't.  might be onto something here...     textField.setText( str );   } } 

And here's the layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="vertical"               android:layout_width="fill_parent"               android:layout_height="fill_parent">   <TextView android:id="@+id/text_field"             android:layout_width="fill_parent"             android:layout_height="wrap_content"/>   <TextView android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:text="@string/unicodechars"/> </LinearLayout> 

Here's strings.xml:

<?xml version="1.0" encoding="utf-8"?> <resources>   <string name="app_name">TestMultibyteActivity</string>   <string name="unicodechars">Tübingen systemportefølje</string> </resources> 

I'm building with ant. Here's my default.properties:

target=Google Inc.:Google APIs:8 

And here's my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.mycompany.android.multibyte"       android:versionCode="1"       android:versionName="1.0">     <application android:label="@string/app_name" android:icon="@drawable/icon">         <activity android:name="TestMultibyteActivity"                   android:label="@string/app_name">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application> </manifest>  

I've tinkered with everything I can think of, but it seems like unicode characters are getting split by the CharSequence interface, and I can't figure out why.

like image 221
Eric Avatar asked Dec 23 '10 20:12

Eric


People also ask

Why do some Unicode characters not show up?

If you are unable to read some Unicode characters in your browser, it may be because your system is not properly configured. Here are some basic instructions for doing that. There are two basic steps: Install fonts that cover the characters you need.

What is the use of SetText in Android?

SetText(String, TextView+BufferType)Sets the text to be displayed using a string resource identifier.


1 Answers

Unfortunately, you just can't do it that way from strings.xml AFAIK.

You're left doing one of two things.

  • Adding the Unicode character within java to the String in the XML file:

    String str = "\u00A9" + getContext().getString(R.string.your_string); 
  • Entering the text as HTML in java:

    yourTextView.setText(Html.fromHtml("your chars")); 

Hope this is useful.

like image 93
user432209 Avatar answered Oct 04 '22 16:10

user432209