Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding in Android TextView

I'm new in Android.
When using Json Parsing, I have a problem with display text(maybe cause by font, I don't know).
This is my Json return:

{"Response":[{"Id":829,"Name":"Tiền không đem lại hạnh phúc nhưng...","ShortDescription":"Một tỷ phú tâm sự với bạn,...  

But when I parse in TextView in android, the "Name" become:

 "Ti�n không đem lại hạnh phúc nhưng..."

This text is in Vietnamese. How can I fix it?

like image 295
Ngo Van Avatar asked Sep 23 '13 04:09

Ngo Van


People also ask

How to change Font size of TextView in android Studio?

Go to File -> Settings, a new setting dialogue box will appear. Then go to Editor -> General. Now mark the checkbox Change font size with Ctrl + Mouse wheel and click on Apply button. Now to change your editor font size, you just have to press and hold Ctrl and rotate the Mouse wheel.

How do I bold text in android XML?

Change Text Style of TextView to BOLD in XML Layout File textStyle attribute of TextView widget accepts on of these values: "bold" , "italic" or "normal" . To change the style to bold, you have to assign textStyle with "bold" .


2 Answers

I fixed it. My problem is UTF-8 charset.

String name = "";
try {
    name = new String(c.getString("NAME").getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {

    e.printStackTrace();
}

String decodedName = Html.fromHtml(name).toString();
like image 196
Ngo Van Avatar answered Oct 31 '22 10:10

Ngo Van


Use a webView to display text like this:

myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);
like image 44
Ryan S Avatar answered Oct 31 '22 09:10

Ryan S