Skip to content Skip to sidebar Skip to footer

String Tag In Xml Is Creating Some Issue In Api Above 16

I am making an app that is in two language English and urdu . I have string file for english and i have string name as When_undressing_ar

Solution 1:

Full working Demo

MainActivity

publicclassMainActivityextendsAppCompatActivity {

    Context context;
    TextView textView;
    SharedPreferences preferences;
    String[] SuppertedLangCodes = {"en", "ur"};
    String key = "lang";

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        preferences = getSharedPreferences("myprefs", MODE_PRIVATE);

        textView = (TextView) findViewById(R.id.foodName);
        textView.setText(getResources().getText(R.string.txt_name));
        textView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                if (preferences.getString("lang", "").toString().equalsIgnoreCase("") || preferences.getString("lang", "").toString().equalsIgnoreCase(SuppertedLangCodes[0])) {

                    applyLanguage(context, SuppertedLangCodes[1]);
                    preferences.edit().putString(key, SuppertedLangCodes[1]).apply();
                } else {

                    applyLanguage(context, SuppertedLangCodes[0]);
                    preferences.edit().putString(key, SuppertedLangCodes[0]).apply();
                }
            }
        });
    }

    publicvoidapplyLanguage(Context context, String language) {
        android.content.res.Configuration config = new android.content.res.Configuration();
        // Since API level 17 or below cause issues with RTL, lets keep them LTRif (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            config.locale = newLocale("en");
        } else {
            config.locale = newLocale(language);
        }
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        recreate();
    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/base"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="10dp"android:background="@color/white"android:orientation="horizontal"android:weightSum="10"><TextViewandroid:id="@+id/foodName"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:gravity="left"android:inputType="textCapWords"android:textColor="@color/colorPrimaryDark"android:textColorHint="@color/colorPrimaryDark"android:textSize="18sp"android:layout_marginLeft="20dp" /></RelativeLayout>

Strings.xml normal

<resources><stringname="app_name">Gallery Test</string><stringname="txt_title_photo_gallery">Photo Gallery</string><stringname="txt_name">what to pray while undressing</string></resources>

Strings.xml from ur folder

<?xml version="1.0" encoding="utf-8"?><resourcesxmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"><!--<string name="txt_name" translatable="false">pk.wiseapps.texter.sms.ps.GSM_SMS</string>--><stringname="txt_name">لباس_اتارتے_وقت_کیا_پڑھیں</string></resources>

enter image description here

enter image description here

Post a Comment for "String Tag In Xml Is Creating Some Issue In Api Above 16"