Arabic text in Android ListView

825 Views Asked by At

I am trying to create a simple Listview in android, with arabic text in each item. The problem is, the displayed text is totally corrupted and not arabic. I have set my Android Studio encoding to UTF-8 but that did not help.

Below is my android code

public class CategoryActivity extends AppCompatActivity {

    // Array of strings...
    String[] mobileArray = {"الرياضي","عربي وعالمي","الثقافي","دنيا","الامارات"};
    //String[] mobileArray ={"zaid", "ahmad", "abdallah"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);

    }
}

Below are my xml files,

activity_category.xml

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

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

activity_listview.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:gravity="right">
</TextView>

Note: Hardcoding the string array in the xml file works, but I don't want that. I want to fetch the array from database at runtime.

1

There are 1 best solutions below

0
On

You put array in values.xml with <string-array>.

put your arabic array inside string.xml in the values-ar folder inside the res.

You can allow RTL from your AndroidMenifest.xml by using "android:supportsRtl="true" with <application> tag.