Tran Hoai Nam Tran Hoai Nam - 3 months ago 9
Android Question

Android EditText does not return the correct UTF-8 text

I have an Edittext to fill in the search string:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_input_edittext"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" />

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:background="@drawable/add_to_cart_btn"
android:text="@string/search"
android:id="@+id/search_search_btn"
android:stateListAnimator="@null"
android:layout_gravity="center_horizontal" />
</LinearLayout>


But the Vietnamese word that I type in is not properly
getText().getString()
. It is supposed to be (I typed this one manually)


củ


but what I got from the code:

EditText search_edittext = (EditText) v_iew.findViewById(R.id.search_input_edittext); //v_iew is from inflating for `AlertDialog`
search_edittext.getText().toString();


is like the image below:

enter image description here

I think they are differently encoded. The hook must be above the "u". But if I copy the whole word and paste to any editor, it shows exactly the same. This is what it looks like when I paste it here:


củ


and this is what happen if I copy the hook only


̉


They look the same but what from the
EditText
cannot be used for further works.

If I copy the word from the internet and paste into the search field, it works! So it's my input method that caused the problem. Tried to search for converting them to unicode (the one with \xxxx) but no luck.

Anyone has any idea on how to convert the input to "normal format"? Thank you for your time!

Answer

If you're having trouble with sending the right formatted text through API, just set UTF(Assuming, its an UTF font you're using) support in HttpClient. Don't worry about what shows in Log. Custom fonts are prone to render incorrectly at Log.

HttpPost post = new HttpPost(url);
StringEntity stringEntity = new StringEntity(payload, "UTF-8");

post.setEntity(stringEntity);
HttpResponse response = httpclient.execute(httppost);

Update: If only part of URL needs to be encoded then use it like following,

String url = URLEncoder.encode(params[0], "utf-8");
HttpPost post = new HttpPost(url);
//Rest of the code 
Comments