Search code examples
javaandroidandroid-toolbarandroid-appbarlayout

use custom title in toolbar with back button


I want custom text in my toolbar title. I have added it as below

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:titleTextColor="?attr/TextColor">
        <TextView
            android:textColor="?attr/TextColor"
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_gravity="center" />

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

and in java I have written codes like below

toolbar = (Toolbar) findViewById(R.id.toolbar);
        Typeface font=Typeface.createFromAsset(AuthorQuoteActivityTabs.this.getAssets(), constant.font);
        TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        mTitle.setText("authorName");
        mTitle.setTypeface(font,Typeface.BOLD);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

its working fine but I am getting custom title text as well app name in toolbar. I want only my custom text in it. How can I remove default title and use only custom text in toolbar ?

Thanks


Solution

  • as you are setting the title in a separate TextView you need to remove the "normal" title by

    getSupportActionBar().setTitle("");