1

I have a toolbar and i want to align the text to left instead of right.

The Main.xml file code is:

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/main_page_toolbar"
            layout="@layout/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        </include>

The code of app_bar_layout.xml is:

   <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/grey_border_bottom"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:titleTextColor="@color/link_blue">

    </android.support.v7.widget.Toolbar>

And the relevant code in Main java file is:

  mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
        setSupportActionBar(mToolbar);
        final String phoneLanguage = Locale.getDefault().toString();
        if (phoneLanguage.equals("iw_IL"))
        {
            getSupportActionBar().setTitle("פוליטיקה");
        }
        else
        {
            getSupportActionBar().setTitle("Politico");
        }

4 Answers 4

1

Do not set title Just add the TextView in Toolbar

Here is demo

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


    </android.support.v7.widget.Toolbar>
2
  • Hi, if i put in a comment all the code in java file and add the Textview as you suggested, i got just empty white toolbar without any title....
    – Dima Bokov
    Commented Oct 9, 2018 at 5:02
  • My textColor was white and this is the reason for text invisibility. tnx a lot!
    – Dima Bokov
    Commented Oct 9, 2018 at 5:23
1

Since your text is in Hebrew the Toolbar by default supports RTL for all RTL supported languages. I guess you are asking to override the RTL behaviour of your text.

In order to make your text left align add this attribute to your Toolbar

android:layoutDirection = "ltr"
1
  • My textColor was white and this is the reason for text invisibility. tnx a lot!
    – Dima Bokov
    Commented Oct 9, 2018 at 5:23
0

include Textview in your app_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:textColor="#000000"
            android:text="@string/app_name"
            android:gravity="right"
            />
    </android.support.v7.widget.Toolbar>
2
  • My textColor was white and this is the reason for text invisibility. tnx a lot!
    – Dima Bokov
    Commented Oct 9, 2018 at 5:23
  • yeah, you just changed the text color of textview
    – Samiran
    Commented Oct 9, 2018 at 5:27
0

If your entire app should be in RTL, you should go to styles.xml and add this to the AppTheme:

 <item name="android:layoutDirection">rtl</item>

And, of course, do not forget to add android:supportsRtl="true" under the application tag in your AndroidManifest.xml file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.