I have a custom Action Bar
XML
layout file in my Android
project:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layoutDirection="rtl"
android:orientation="horizontal"
android:textDirection="rtl">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Some Text"
android:textColor="@color/white" />
</LinearLayout>
Now if try to load and display this Action Bar
like this:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.id.custom_action_bar);
The layout gets loaded as RTL
and text direction is RTL
, and everything works fine, but I wanted to make the title dynamic so I decided to inflate
the layout file into a View
, and change the text of the TextView
like this:
View customActionBar = LayoutInflater.from(this)
.inflate(R.layout.custom_action_bar, null);
TextView actionBarTitle = customActionBar.findViewById(R.id.tv_name);
actionBarTitle.setText("Any Text");
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(customActionBar);
But then the layout and text direction become LTR
. I tried to set the direction to RTL
programmatically, and that didn't work too.
What is wrong here? What should I do to fix it?
You need to add android:supportRtl="true"
in the AndroidManifest.xml to have rtl layout