3

I know it seems a dumb question that has should be already asked, but I did not found the question simply asked and answered.

My manifest has in first line:

 <?xml version="1.0" encoding="utf-8"?>

If I have xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_target"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:layout_margin="10dp"
        android:text="" />
    </LinearLayout>

and java code :

 TextView question = (TextView) findViewById(R.id.text_target);
 String text = "Lorsqu'ils sont utilisés, les accents peuvent être un plus à apporter à leur texte.";
 question.setText(question.setText(Html.fromHtml(text)));

It displays:

enter image description here

Do you know how to display accent?

lambdaDev
  • 500
  • 4
  • 18

5 Answers5

3

When the java source text is in some non-ASCII encoding, then the editor and javac compiler need to use the same encoding.

The first check is to use u-escaped special chars: try \u00e9 instead of é to see whether it is a problem that the editor uses another encoding than the compiler.

In the above one char became a placeholder char. This means that the editor is probably using Windows Latin-1 (typical for French) and javac is using UTF-8.

Both should be consistently the same. And for internationalization purposes UTF-8 is nicest.

The java source code then would need to be converted. A programmer's editor like Notepad++ or JEdit can do that. Also the java tool native2ascii

native2ascii -encoding Cp1252 X.java X-a.java
native2ascii -reverse -encoding UTF-8 X-a.java X.java

The first u-escapes all special chars, the second makes UTF-8 chars.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

This happen because you are setting this text as English text and emulator recognize this as English only and the words that he doest not understand prints as given.

To avoid you have set typeface of this text as the language what is displayed here. This issue is due to uni coding format

Amarjit
  • 4,327
  • 2
  • 34
  • 51
  • i thought to it and i checked that I have IDE encoding and Project encoding set at UTF-8 in Android studio, so how change the uni coding format? – lambdaDev Jul 28 '15 at 09:55
  • I got this problem when i am using some regional fonts, So you can set fonts – Amarjit Jul 28 '15 at 09:58
  • Your IDE and project encoding will not work for device or emulator. For those you have to set different encoding using typeface – Amarjit Jul 28 '15 at 09:59
0

u must set the font. fontTextView.setTypeface(mFace);

0

If you are getting strange results with certain characters in different locales, make sure you're not using fonts that don't support those characters. Fall back to not using a font (don't call setTypeface) and test that way.

0

This worked for me:

myVariable.setText("% de usuarios que dicen que regresar"+Html.fromHtml("&aacute;")+"n ("+Integer.toString(productDetail.surveyresponses)+" respuestas)");

I could use the &aacute; of HTML to produce the á that I needed.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103