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.
By default, the action bar contains just the name of the app and an overflow menu.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With