1

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);
Muhammad Abdullah
  • 271
  • 1
  • 2
  • 15

2 Answers2

2

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

Community
  • 1
  • 1
Rajesh
  • 2,618
  • 19
  • 25
  • why set typeface after set text? – Muhammad Abdullah Nov 03 '15 at 04:23
  • btw it didn't worked out. {"success":"100","message":"successful","name":"\u0645\u062d\u0645\u062f \u0639\u0628\u062f\u0627\u0644\u0644\u0647 \u0627\u0639\u0638\u0645 \u062e\u0627\u0646"} This is the exact return from API, stored in a string, and the \u062f\ etc parts are urdu characters. – Muhammad Abdullah Nov 03 '15 at 04:27
  • it doesn't matter whatever you store, if any word/chareter from string is in UTF/html format then it will automatically convert to html\ – Rajesh Nov 03 '15 at 04:30
  • I have tried the second code you posted but the app crashed for unknown reason. Couldn't find anything on logcat as well – Muhammad Abdullah Nov 03 '15 at 04:55
  • let me try will give you solution soon – Rajesh Nov 03 '15 at 04:56
0

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

Community
  • 1
  • 1
walkmn
  • 2,322
  • 22
  • 29