3

I found this answer which perfectly fit my question. But it does not work for me. Don't know why. Maybe I'm doing something wrong.

I copied exactly the code from the answer. So in my com.example.printhelper package is Application.java a FontsOverride.java files. In res/assets/fonts I have Zawgyi_One.ttf font. And I have this line:

<item name="android:typeface">serif</item>

in theme.xml.

Some Myanmar words are stored in my database (UTF-8) and I get them through json by http get.

But I still saw (ñöõ)½ºýüþºû±ý(ôð)þ±à èºî on my EditText instead of Myanmar letters.

Am I doing something wrong? I use Android Studio editor.

CODE: Application.java

package com.example.printhelper;

public final class Application extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FontsOverride.setDefaultFont(this, "DEFAULT", "Zawgyi_One.ttf");
        FontsOverride.setDefaultFont(this, "DEFAULT_BOLD", "Zawgyi_One.ttf");
        FontsOverride.setDefaultFont(this, "MONOSPACE", "Zawgyi_One.ttf");
        FontsOverride.setDefaultFont(this, "SERIF", "Zawgyi_One.ttf");
        FontsOverride.setDefaultFont(this, "SANS_SERIF", "Zawgyi_One.ttf");
    }
}

FontsOverride.java

public final class FontsOverride {

    public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
        try {
            final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
            staticField.setAccessible(true);
            staticField.set(null, newTypeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

NOTE: I Want to override all TextView and EditText

Still no answer. I guess there is something in my application which is blocking the font, but I have no idea what and why. I have several themes in my aplication which can user choose. All styled in styles.xml and themes.xml. All themes inerits from the same parent. It's ok to declare the font style only for parent, right?

EDIT: Cathing the json - I use AsyncTask

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpget,responseHandler);

EDIT 2: String conversion

I convert response to hex

HttpGet httpget = new HttpGet(url);
httpget.setHeader("Content-type", "application/json; charset=utf-8");;
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpget,responseHandler);
byte[] array = httpclient.execute(httpget,responseHandler).getBytes();
StringBuffer hexString = new StringBuffer();
for (byte b : array) {
    int intVal = b & 0xff;
    if (intVal < 0x10)
        hexString.append("0");
    hexString.append(Integer.toHexString(intVal));
}
System.out.println(hexString);

and this is what it throws

7b22766965776572735f6964223a33352c22766965776572735f66697273746e616d65223a224b41222c22766965776572735f6c6173746e616d65223a22204b412053455420333528437573746f6d657220c382c2bdc383c2a2c382c2b6c383c2bdc382c2a6c383c2a0c383c2a8c383c2adc382c2b6c383c2a0c382c2a5c383c2bdc383c2bcc382c2bcc383c28cc383c2bd29222c22766965776572735f7374726565746e616d65223a22c383c282c382c2bac383c2bbc382c2b1c383c2bd20c383c2b4c383c2b3c383c2bec382c2bfc383c2a3c382c2bcc383c2a2c382c2bac382c2a3c3a2c280c29cc382c2bdc383c2a020c382c2bdc382c2bac383c2bdc383c2bcc382c2bfc383c2aec382c2bbc383c2bd20c383c2b2222c22766965776572735f686f7573656e756d223a6e756c6c2c22766965776572735f63697479223a6e756c6c2c22766965776572735f706f7374636f6465223a6e756c6c2c22766965776572735f6372656174696f6e5f74696d65223a7b2264617465223a22323031322d31312d31382031373a30313a30362e303030303030222c2274696d657a6f6e655f74797065223a332c2274696d657a6f6e65223a22417369612f52616e676f6f6e227d2c22766965776572735f69735f616374697665223a312c22766965776572735f6e6f7465223a6e756c6c2c22766965776572735f736d617274636172645f6964223a313435303030303033332c22766965776572735f736574746f70626f785f6964223a6e756c6c2c22766965776572735f636f6e74726163745f6e756d626572223a22222c22766965776572735f656d61696c223a6e756c6c2c22766965776572735f70686f6e656e756d223a3231313535342c22766965776572735f68705f6e756d626572223a6e756c6c2c22766965776572735f6d61726974616c5f737461747573223a2273696e676c65222c22766965776572735f646174655f6f665f61637469766174696f6e223a6e756c6c2c22766965776572735f616765223a302c22766965776572735f6f636375706174696f6e223a6e756c6c2c22766965776572735f6e69665f63696e223a6e756c6c2c22766965776572735f7a6f6e65223a6e756c6c2c22766965776572735f67656e646572223a226d616c65222c22766965776572735f70686f6e656e756d32223a6e756c6c2c22766965776572735f637265617465645f75736572735f6964223a6e756c6c2c22766965776572735f6163746976617465645f75736572735f6964223a6e756c6c2c22766965776572735f656469746564223a6e756c6c2c22766965776572735f6469766973696f6e735f6964223a6e756c6c2c22766965776572735f746f776e73686970735f6964223a6e756c6c2c22766965776572735f6465616c6572735f6964223a6e756c6c2c22766965776572735f626f75717565745f6578706972655f64617465223a7b2264617465223a22323031312d31312d31312030303a30303a30302e303030303030222c2274696d657a6f6e655f74797065223a332c2274696d657a6f6e65223a22417369612f52616e676f6f6e227d2c22766965776572735f6f70656e65645f7469636b6574223a302c22766965776572735f736574746f70626f785f726570616972223a302c22766965776572735f656469745f74696d65223a6e756c6c2c22766965776572735f656469745f75736572735f6964223a6e756c6c2c22766965776572735f73656c6663617265223a7b2264617465223a222d303030312d31312d33302030303a30303a30302e303030303030222c2274696d657a6f6e655f74797065223a332c2274696d657a6f6e65223a22417369612f52616e676f6f6e227d2c22766965776572735f6269727468646179223a6e756c6c7d

Here you could convert hex to string.

Does it means the problem is on server?

Community
  • 1
  • 1
kristyna
  • 1,360
  • 2
  • 24
  • 41
  • Why are you setting typeface "serif" instead of typeface "Zawgyi One" or whatever that font says its family name is? – Mike 'Pomax' Kamermans Jul 12 '15 at 16:44
  • @Mike'Pomax'Kamermans Look at the question I mentioned. FontsOverride.java overrides default font. `FontsOverride.setDefaultFont(this, "DEFAULT", "Zawgyi_one.ttf");` – kristyna Jul 12 '15 at 17:13
  • worth putting that in your own post too, then. I can't tell what parts you actually copied and which parts you haven't: can you add your code to your post (ideally not just that one line, but simply all the code that's relevant to your question)? – Mike 'Pomax' Kamermans Jul 12 '15 at 17:22
  • @Mike'Pomax'Kamermans Of course. See edit. – kristyna Jul 12 '15 at 17:34
  • Nice. Also, a google search for "android setDefaultFont" finds me https://gist.github.com/artem-zinnatullin/7749076, which sounds very much like what you're trying to do - does that address your problem? – Mike 'Pomax' Kamermans Jul 12 '15 at 17:46
  • @Mike'Pomax'Kamermans no. It seems not working. I copied the code, but nothing happend. I tried to use different .ttf file with no change... – kristyna Jul 12 '15 at 18:37
  • 1
    that's not a font issue that's an encoding issue. Your json get probably messes things up. – njzk2 Jul 24 '15 at 12:13
  • @njzk2 Do you mean that json which I'm getting from the browser is encoded badly, so that's why the same appears on mobile? – kristyna Jul 24 '15 at 12:16
  • @kikinet exactly. The characters you are seeing are typical of an utf-8 string read as some sort of iso. Post the code where you receive the json string. – njzk2 Jul 24 '15 at 13:29
  • @njzk2 see the edit please – kristyna Jul 27 '15 at 09:11
  • since the client you use should be able to use the content encoding given by the server, it is probable that your server is messing up something, or that the string returned by the server is already garbled. Do you have access to the server or the people who wrote it? – njzk2 Jul 27 '15 at 13:20
  • @njzk2 Yes I have. What should I do? I tried to set charset with the HttpGet in asyncTask with no change... – kristyna Jul 27 '15 at 20:43
  • you need to make sure the content type and the values sent by the server are correct. Typically, it would involve getting the raw bytes of the response, trying to decode that as UTF-8, seeing what it gives. Then slapping the server people until they fix their encoding – njzk2 Jul 27 '15 at 22:13
  • @njzk2 Thank you. I tried it, in my update is my attemp do to this. Please check out, if it is corect. – kristyna Jul 28 '15 at 08:25

1 Answers1

0

I've fix it already by using application/json; charset=utf-8. I was looking for problem in my android app, but it was on the sending side.

I'm using Nette Framework, so it wasn't as clear as it sounds. Exactly the part which needed update was:

JsonResponse($data, 'application/json; charset=utf-8'));

Yes, from the comment

that's not a font issue that's an encoding issue

. Thanks to @njzk2 for help.

kristyna
  • 1,360
  • 2
  • 24
  • 41