Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In android app Toolbar.setTitle method has no effect – application name is shown as title

I'm trying to create simple application using android-support-v7:21 library.

Code snippets:
MainActivity.java

public class MainActivity extends ActionBarActivity {      Toolbar mActionBarToolbar;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);                     mActionBarToolbar.setTitle("My title");         setSupportActionBar(mActionBarToolbar); } 

activity_main.xml

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fitsSystemWindows="true"     android:orientation="vertical">      <android.support.v7.widget.Toolbar                     android:id="@+id/toolbar_actionbar"         android:background="@null"         android:layout_width="match_parent"         android:layout_height="?actionBarSize"         android:fitsSystemWindows="true" />  </LinearLayout> 

But instead of "My title" on Toolbar %application name% is shown.
Seems like setTitle method has no effect.
I would like to show "My title".

UPD: Before, styles.xml was:

<style name="AppTheme" parent="Theme.AppCompat">     <item name="windowActionBar">false</item> </style> 

So, I thought that actionbar is not used. I add NoActionBar to style parent:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">     <item name="windowActionBar">false</item> </style> 

But the problem is not resolved.

like image 381
krossovochkin Avatar asked Oct 21 '14 12:10

krossovochkin


People also ask

Which Toolbar show the name of the application?

By default, the action bar contains just the name of the app and an overflow menu.

What is action bar and title bar in android?

The Title bar is a small part of the UI that you can supply with some text and a color. You see it on a lot of Android 2.0 Apps. See here. The Actionbar is the bar with buttons that has back navigation etc. If you can chose, you use it instead of the Titlebar.

What is Toolbar in android app?

In Android applications, Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar.


2 Answers

Found the solution:

Instead of:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);             mActionBarToolbar.setTitle("My title"); setSupportActionBar(mActionBarToolbar); 

I used:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);             setSupportActionBar(mActionBarToolbar); getSupportActionBar().setTitle("My title"); 

And it works.

like image 116
krossovochkin Avatar answered Oct 16 '22 21:10

krossovochkin


For anyone who needs to set up the title through the Toolbar some time after setting the SupportActionBar, read this.

The internal implementation of the support library just checks if the Toolbar has a title (not null) at the moment the SupportActionBar is set up. If there is, then this title will be used instead of the window title. You can then set a dummy title while you load the real title.

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); mActionBarToolbar.setTitle(""); setSupportActionBar(mActionBarToolbar); 

later...

mActionBarToolbar.setTitle(title); 
like image 39
sorianiv Avatar answered Oct 16 '22 23:10

sorianiv