2

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

Priya
  • 1,602
  • 4
  • 22
  • 37
  • If you want centered text in toolbar, just putting another `TextView` inside `Toolbar` might be not enough. Text position will depend on other items in a toolbar (back button, menu items). You can take a look at this question: https://stackoverflow.com/questions/29443604/android-how-to-center-title-in-toolbar It contain more robust implementations of centered toolbar – user2137020 Oct 04 '17 at 19:54

2 Answers2

3

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

getSupportActionBar().setTitle("");
Christian
  • 4,596
  • 1
  • 26
  • 33
1

Please use this code:

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

Hope it helps,

Crono
  • 2,024
  • 18
  • 16