2

I am working on xml in my android project and I am a beginner. i have a same xml in different language too like that

<sura index="1" name="الفاتحة">
    <aya index="1" text="الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ" bismillah="بِسْمِ اللَّهِ الرَّحْمَنِ 
    الرَّحِيمِ" />
    <aya index="2" text="الرَّحْمَنِ الرَّحِيمِ" />
    <aya index="3" text="مَالِكِ يَوْمِ الدِّينِ" />
    <aya index="4" text="إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ" />
    <aya index="5" text="اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ" />
    <aya index="6" text="صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ" />
</sura>

this one is in arabic

<sura index="1" name="الفاتحة">
    <aya index="1" text="In the name of Allah, most benevolent, ever-merciful."/>
    <aya index="2" text="ALL PRAISE BE to Allah, Lord of all the worlds,"/>
    <aya index="3" text="Most beneficent, ever-merciful,"/>
    <aya index="4" text="King of the Day of Judgement."/>
    <aya index="5" text="You alone we worship, and to You alone turn for help."/>
    <aya index="6" text="Guide us (O Lord) to the path that is straight,"/>
    <aya index="7" text="The path of those You have blessed, Not of those who have earned Your anger, nor those who have gone astray."/>
</sura>

this one in english.

I have successfully done parsing for aya in arabic using this code

private List<String> getAllAyaFromSuraIndex(String suraIndex) throws XmlPullParserException, IOException {
     list = new ArrayList<>();

    XmlPullParser parser = getResources().getXml(R.xml.quran_arabic);
    while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("sura")) {
            String index = parser.getAttributeValue(0);

            // Match given sure index
            if (index.equals(suraIndex)) {
                // Move to first aya tag inside matched sura index
                parser.next();

                // This loop will exit when it reaches sura end tag </sura>
                while (parser.getName().equals("aya")) {
                    if (parser.getEventType() == XmlPullParser.START_TAG) {
                        list.add(parser.getAttributeValue(null,"index") + ".\n" + parser.getAttributeValue(null,"text"));
                    }

                    // Move to next aya tag
                    parser.next();
                }

                break;
            }
        }

        parser.next();
    }

    return list;
}

I have a listview with two textviews one for arabic and one for english. I tried everything but not getting the results.

Khurram Ansar
  • 97
  • 1
  • 10
  • Related https://stackoverflow.com/a/53593870/7666442 – AskNilesh Mar 06 '19 at 16:35
  • Im using 2 different xmls. The above example is working on xml using tags i have same 2 different xmls just in different language and text is same. I am asking how can i work 2 different xmls for same tag at a time. @NileshRathod – Khurram Ansar Mar 06 '19 at 16:43

1 Answers1

2

When you are developing multilingual application the best way to handle translations in android is to define content in the locale category. Let me explain it you

1st Step create directory with locale names mentioned values-(language) e.g values-ar

MyProject/
          res/
              values-es/
                        strings.xml
              values-ar/
                        strings.xml

values-es/strings.xml

 <resources>
     <string name="ayah1">ALL PRAISE BE to Allah, Lord of all the worlds,.</string>
     <string name="ayah2">Most beneficent, ever-merciful,</string>
 </resources>

values-ar/strings.xml

 <resources>
     <string name="ayah1">الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ</string>
     <string name="ayah2">الرَّحْمَنِ الرَّحِيمِ</string>
 </resources>

2nd Step now use these strings in your view files as

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.javapapers.multilingualapp.MainActivity">

    <TextView
        android:id="@+id/string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ayah1"                 //here the values being used
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</android.support.constraint.ConstraintLayout>

3rd and last step now whenever you want to change the language just change locale using the following function

public void setLocale(String localeName) {
        if (!localeName.equals(currentLanguage)) {
            myLocale = new Locale(localeName);
            Resources res = getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.locale = myLocale;
            res.updateConfiguration(conf, dm);
            Intent refresh = new Intent(this, MainActivity.class);
            refresh.putExtra(currentLang, localeName);
            startActivity(refresh);
        } else {
            Toast.makeText(MainActivity.this, "Language already selected!", Toast.LENGTH_SHORT).show();
        }
    }
Muhammad Ibrahim
  • 229
  • 4
  • 16
  • Oh , I am sorry but I think you got me wrong. I am talking about xml parsing to show data in my app. I have 2 xmls of full Quran in two languages. when user click the specific surah, All Ayats of specific surah will open and they will in 2 languages I.e arabic and English. I want to parse both xmls using XMLPullParser to show in List View . In my above I am successfully parsing arabic language but how can i parse second xml too at that time? – Khurram Ansar Mar 07 '19 at 09:35