androidtoolbartitleandroid-titlebar

How to change title's position in toolbar?


I have a toolbar in my activity (actionbar app is removed). In my toolbar, I have a title and icon. Now I want to move the title to the right with this code: android:layoutDirection="rtl" but the icon is also moved. I want to move only the title to the right.

Toolbar code:

    <style name="ToolBar" parent="AppTheme">

    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColorSecondary">@color/colorToolbar</item>
    <item name="titleTextColor">@color/colorToolbar</item>
    <item name="android:background">?attr/colorPrimary</item>
    <item name="android:layoutDirection">rtl</item>

</style>

Activity.java :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_addNewItem);
        setSupportActionBar(toolbar);

    if(getSupportActionBar() != null){
        getSupportActionBar().setTitle("کلاس جدید");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

enter image description here


Solution

  • You have to make TextView inside toolbar and set gravity as you want. for your problem it is not necessary to take toolbar, you can also use layout. This is my coordinate layout. In which you can easily adjust TextView inside toolbar.

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/gradientcolor"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/AppTheme.PopupOverlay">
        <!-- app:layout_scrollFlags="scroll|enterAlways"-->
        <TextView
            android:id="@+id/toolbar_title"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Dhakar Matrimonial"
            android:textColor="@android:color/white"
            android:textSize="@dimen/middium_text_size" />
    
        <ImageView
            android:id="@+id/iv_chat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="@dimen/middium_padding"
            android:padding="@dimen/small_padding"
            android:src="@android:drawable/stat_notify_chat" />
    
        <ImageView
            android:id="@+id/iv_universerch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:padding="@dimen/small_padding"
            android:src="@mipmap/search" />
    
    </android.support.v7.widget.Toolbar>