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.
I have a strange behaviour that may can help you.
This is working but it has no effect in onCreate only:
Try to use this in onCreate:
This can be done by setting the
android:label
attribute of your activity in theAndroidManifest.xml
file:And then add this line to the onCreate():
Though not immediately relevant to this particular setup, I found that removing "CollapsingToolbarLayout" from my XML that was wrapping my toolbar inside of an AppBarLayout made everything work.
So, this:
Instead of this:
Then, I set the title in the activity's onCreate, before setSupportActionBar() is called.
This is happening because you are using Toolbar and ActionBar both. Now as you want to use Toolbar as an action bar, the first thing you need to do is disable the decor provided action bar.
The easiest way is to have your theme extend from
Theme.AppCompat.NoActionBar
.It's not just about
.setTitle
more methods of Support Toolbar (Appcompat v7) in
onCreate
works only withgetSupportActionBar().method()
and don't work with
mToolbar.method()
examples:
though next methods works fine without
getSupportActionBar()
inonCreate
Problem only with
onCreate
event, you still can usemToolbar.setTitle()
later instead of annoyinggetSupportActionBar().setTitle()
, for example if you add this inonCreate
it will work (because it will be executed later, afteronCreate
)I prefer to use this solution https://stackoverflow.com/a/35430590/4548520 than https://stackoverflow.com/a/26506858/4548520 because if you change title many times (in different functions) it's more comfortable to use
mToolbar.setTitle()
than longergetSupportActionBar().setTitle()
one and you don't see annoying notification about null exception like withgetSupportActionBar().setTitle()
I tried to rename the toolbar from the fragment
It helped me, I hope to help someone else
Screenshot:![enter image description here](https://i.stack.imgur.com/cZ7ke.jpg)