I'm trying to handle RTL (Right-to-left) layouts within a complex layout that contains two TextViews and an ImageButton.
However, the layout direction is never returning RTL as the layout direction until some time after the onResume() method has been called. The call to ViewCompat.getLayoutDirection(getView())
always returns LTR at every point in the life cycle I have checked except for in onStop()
We use this method all the time to handle bind viewholders in a RecyclerView but this is the first time we've tried to use it on a complex layout outside of a RecyclerView.
Has anyone else seen this behavior or know how (or when) to get the proper layout direction?
Here is what I am doing to handle RTL:
MyFragment.java:
private TextView title;
private TextView subtitle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_preview_audio, container, false);
loadAsset();
validateAsset();
int layoutDirection = ViewCompat.getLayoutDirection(container);
setupUi(layoutDirection);
populateData();
return view;
}
private void setupUi(int layoutDirection) {
int gravity = GravityCompat.getAbsoluteGravity(GravityCompat.START, layoutDirection);
title.setGravity(gravity);
subtitle.setGravity(gravity);
}
details.xml (included in fragment_preview_audio)
<?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:id="@+id/details_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@color/text_dark_grey"
android:textStyle="bold"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
/>
<ImageButton
android:id="@+id/menu_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_more_vert_black_24dp"
style="?android:attr/buttonBarButtonStyle"
tools:ignore="ContentDescription"/>
</LinearLayout>
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/text_light_grey"
android:singleLine="false"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
/>
Use whichever is the container widget that has sub-components that needs to anticipate layout gravity of start or end, and have it indicated as the parameter inside the method ViewCompat.getLayoutDirection(container)
. I used recyclerview inside my itemdecorator and it finally worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With