How To Apply Rtl Dynamically And Effectively
I'm creating and adding TextViews in linear layout dynamically, whenever I'm applying layoutAmount.setRotationY(180); the layout changes its direction which is right, but the words
Solution 1:
setLayoutDirection will set layout direction according to the language
-Make sure that use layout gravity start and end
publicstaticvoidsetLanguage(Context context, String language) {
String[] localeLang = language.split("_");
Locale locale;
if (localeLang.length > 1)
locale = newLocale(localeLang[0], localeLang[1]);
else
locale = newLocale(localeLang[0]);
Locale.setDefault(locale);
Configuration config = newConfiguration();
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(locale);
((Activity) context).getWindow().getDecorView().setLayoutDirection(config.getLayoutDirection());
}
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
Solution 2:
The best way to make layout dynamic RTL by following,
If you use ConstraintLayout
Don't use left and right
instead, use start and end
Android will change layout dynamically when you change locale
You can find my answer here about how to change the language of the app by locale
Update:
In ConstraintLayout Use Constrains as
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imageView_about_back"
app:layout_constraintTop_toTopOf="parent"
Also, don't use margin left or right
In case of LinearLayout,
use
gravity start and end
sample code:
<LinearLayoutxmlns: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"android:background="@drawable/background_image"tools:context=".activity.HomeActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="start"android:text="@string/about_al_ameen"android:textColor="@color/colorWhite" /></LinearLayout>
Solution 3:
when you choose language add;
LocaleHelper.setLocale(LAngSelect.this, "ar");
Then save it with sqlite or other.
Then go to MainActivity with intent:
on MainActivity get the language saved and add:
if (langs.equals("ar")) {
forceRTLIfSupported();
}
private void forceRTLIfSupported()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
//HERE CHECK CONDITION FOR YOUR LANGUAGE if it is AR then//change if it is english then don'tgetWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
Post a Comment for "How To Apply Rtl Dynamically And Effectively"