Rameez Hussain
Rameez Hussain

Reputation: 6764

Android display foreign language strings

I have an app where I retrieve some data in JSON format. Some of the strings in the JSON file are in foreign languages such as Arabic, Hindi, etc. I've tried decoding the string using UTF-8 but it stills shows gibberish, not the original script. Any idea how I can achieve this?

EDIT: My method of decoding the string from JSON

String result; // result in string format obtained from server

JSONTokener tokener = new JSONTokener(result);

JSONObject obj = new JSONObject(tokener);

String msg = obj.getString("message");

TextView message = TextView) findViewById(R.id.inbox_msg);

try {

message.setText(new String(msg.getBytes(), "UTF-8"));

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Anything wrong I'm doing?

P.S. : I tried using a DroidHindi.ttf file to display the text, but it didn't work. I'm running the app on a Samsung Galaxy Tab 2 7.0

Upvotes: 1

Views: 558

Answers (1)

Ruban
Ruban

Reputation: 1534

Android supports arabic language in default.for hindi in server side you should unicode the hindi text and in client side(android) decode the text and print the value. remember that in both client side and server side u should use same font to unicode and decode the text.(In ex i have used Kruti DEv 10.TTF). http://www.infowebservices.in/hindi/ refer this site to convert hindi text to unicode else use some library.

LinearLayout mainLayout=new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(mainLayout);

        TextView arbicView=new TextView(this);
        arbicView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
        arbicView.setText("الغارديان: انقسام بين سكان الجولان حول نظام الاسد");
        mainLayout.addView(arbicView);


        TextView hindiView=new TextView(this);
        hindiView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
        hindiView.setText("Hghgfdhgfhsdgfh h hhfh hkshfkuifyeryeuiryhdfjkhdfuiyerihjkfhsdjkyruiweyk");
        hindiView.setTypeface(Typeface.createFromAsset(getAssets(), "Kruti Dev 10.TTF"));
        mainLayout.addView(hindiView);

Upvotes: 1

Related Questions