Decoding Urdu Codes in Android

怎甘沉沦 提交于 2019-12-11 11:34:39

问题


I am calling an API and receiving the response in both English and Urdu. The response is stored in a string and the urdu part shows character text like "/u024/".

I have been implementing this code set which is giving same result for as before for urdu characters. Kindly if anyone can help me out on this

    String fontPath = "urdu.ttf";

    // text view label
    TextView txtGhost = (TextView) findViewById(R.id.ghost);

    // Loading Font Face
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

    // Applying font
    txtGhost.setTypeface(tf);

回答1:


Your response in Unicode, try this

TextView tv=(TextView)findViewById(R.id.textViewmyView);
final Typeface tf = Typeface.createFromAsset(this.getAssets(),"asunaskh.ttf");
tv.setTypeface(tf);
tv.setText(Html.fromHtml(yourText);

add this if above doesn't work

String str = myString.split(" ")[0];
str = str.replace("\\","");
String[] arr = str.split("u");
String text = "";
for(int i = 1; i < arr.length; i++){
int hexVal = Integer.parseInt(arr[i], 16);
text += (char)hexVal;
}

or this

textview.setText(Html.from(StringEscapeUtils.unescapeJava(unicode))); //this method

for more : see this How to use unicode in Android resource?

How to convert a string with Unicode encoding to a string of letters




回答2:


Try this font instead your current font: http://www.quran.or.kr/urdu/font/asunaskh.ttf

try {
    txtGhost.setTypeface(Typeface.createFromAsset(this.getAssets(),"asunaskh.ttf"));
    txtGhost.setText("ur text");
}
catch(Exception ex) {
    Log.e("TAG", ex.toString());
}

And, what errors did you get in your case?

More information about language support: how to add language support to android



来源:https://stackoverflow.com/questions/33477205/decoding-urdu-codes-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!