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.
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.
later...
Found the solution:
Instead of:
I used:
And it works.
To set the title for each Navbar fragment title
I made it work by using -
The answer is in the documentation (which you can find here):
That is the solution you actually found. Just thought of giving a reference to the official documentation (which apparently few tend to read).
Simply you can change any acitivity name by using