Android Font in more than one language on single screen

前端 未结 1 931
不知归路
不知归路 2021-01-27 08:43

I am new to Android. And I am implementing an application which will be in two languages English and Urdu. Basically there will be some text in arabic and its meaning will be in

相关标签:
1条回答
  • for example if you want your application to support both English and Arabic strings (in addition to the default strings), you can simply create two additional resource directories called /res/values-en (for the English strings.xml) and /res/values-ar (for the Arabic strings.xml).

    Within the strings.xml files, the resource names are the same.

    For example, the /res/values-en/strings.xml file could look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello">Hello in English!</string>
    </resources>
    

    Whereas, the /res/values-ar/strings.xml file would look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello">مرحبا في اللغة الإنجليزية</string>
    </resources>
    

    also , the /res/values-ur_IN/strings.xml file would look like this for urdu:

    ur_IN for india ur_PK for pakisthan

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="hello">انگریزی میں خوش!!</string>
    </resources>
    

    A default layout file in the /res/layout directory that displays the string refers to the string by the variable name @string/hello, without regard to which language or directory the string resource is in.

    The Android operating system determines which version of the string (French, English, or default) to load at runtime.A layout with a TextView control to display the string might look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/hello" >
    </LinearLayout>
    

    The string is accessed programmatically in the normal way:

    String str = getString(R.string.hello);
    

    For change the language you need to like that change lang..

    btn_english.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Locale locale = new Locale("en"); 
                      Locale.setDefault(locale);
                      Configuration config = new Configuration();
                      config.locale = locale;
                      getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                      Toast.makeText(this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
    
                }
            });
    
    
    
     btn_arbice.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                     Locale locale = new Locale("ar"); 
                      Locale.setDefault(locale);
                      Configuration config = new Configuration();
                      config.locale = locale;
                      getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                      Toast.makeText(this, getResources().getString(R.string.lbl_langSelecURdu), Toast.LENGTH_SHORT).show();
    
                }
            });
    
    
     btn_urdu.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Locale locale = new Locale("ur_IN"); 
                      Locale.setDefault(locale);
                      Configuration config = new Configuration();
                      config.locale = locale;
                      getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                      Toast.makeText(HomeActivity.this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
    
                }
            });
    

    Also see this example for single screen

    Custom fonts for TextView based on languages inside String

    String paragraph = "hey what's up ضعيف"; int NO_FLAG = 0; Bidi bidi = new Bidi(paragraph, NO_FLAG); int runCount = bidi.getRunCount(); for (int i = 0; i < runCount; i++) { String ltrtl = bidi.getRunLevel(i) % 2 == 0 ? "ltr" : "rtl"; String subString = paragraph.substring(bidi.getRunStart(i), bidi.getRunLimit(i)); Log.d(">>bidi:" + i, subString+" is "+ltrtl); }

    prints:

    hey what's up is ltr

    ضعيف is rtl

    So now one can easily build TypefaceSpan or MetricAffectingSpan based on language direction like this:

    SpannableString spanString = new SpannableString(paragraph);
    for (int i = 0; i < runCount; i++) {
        Object span = bidi.getRunLevel(i) % 2 == 0 ? ltrFontSpan : rtlFontSpan;
        spanString.setSpan(span, bidi.getRunStart(i), bidi.getRunLimit(i), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    }
    textView.setText(spanString);
    
    0 讨论(0)
提交回复
热议问题