Doronz
Doronz

Reputation: 704

Manipulating the Toolbar's title and other elements

I'm using the AppCompat toolbar, and for the most part its great. However, dealing with the title has been a nightmare. It's set to the app name by default, and I cannot find a way to hide it.

I was trying to hide it so that I can add my own textview so that I could properly align the title text to the top currently its left-aligned but vertically centered. I could not figure out how to position it where it would be normally if I didn't make my toolbar height longer than usual.

How have you guys been managing the toolbar? How can I align the title text to be at the top position where the title's normally are?

Edit: I've added a screenshot of what it looks like. I'm trying to keep my project private for now so I erased any identifying elements. But notice that the D (which is the title) is not aligned to the top.

toolbar

Upvotes: 1

Views: 2455

Answers (3)

Nooh
Nooh

Reputation: 1546

to change Toolbar title try this

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 getSupportActionBar().setTitle("Text");

for custom layout for Toolbar try this

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.actionbar); //replace with your custom toolbar title

Upvotes: 0

Anirudh Sharma
Anirudh Sharma

Reputation: 7974

If you have set your toolbar as action bar setSupportActionBar(toolbar); then you can sipmly disable the title by using

getSupportActionBar().setDisplayShowTitleEnabled(false);

and add a textview as your Title in your toolbar layout as toolbar is also a view. See following

<LinearLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <android.support.v7.widget.Toolbar
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/material_blue"
            android:elevation="5dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
            app:theme="@style/ToolbarCustomIconColor" >

            <TextView
                android:id="@+id/textview_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center"
                android:text="@string/app_name"
                android:textColor="@android:color/white" />
        </android.support.v7.widget.Toolbar>
    </LinearLayout>

This way you can arrange your title anywhere and customize accordingly.

Upvotes: 3

user4571931
user4571931

Reputation:

You are using ToolBar from support v7 AppCompat after that set that toolbar as action bar and then set action bar title for simple title or you want to set your custom layout inside toolbar then same as we are doing in action bar hence.. now your toolbar work as a actionbar..

See below example from ActionBarActivity..

 mToolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(mToolbar);
 getSupportActionBar().setTitle("Your Title");

Now if you do anything with action bar related methods it is ultimately affected in ToolBar.

Upvotes: 0

Related Questions