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 (@sorianiv) AND your
Toolbar
is inside aCollapsingToolbarLayout
, read this:Then later,
If you are using
CollapsibleToolbarLayout
along withToolbar
then you will need to set title in both the layoutsset your Toolbar as action bar in
onCreate
methodLater simply update title of toolbar using
setTitle
methodTry this .. this method works for me..!! hope it may help somebody..!!
To display logo in toolbar try the below snippet. // Set drawable
Let me know the result.
Please see https://code.google.com/p/android/issues/detail?id=77763. Apparently it is supposed to work like that. Once you call the
setSupportActionBar()
method call, it then is the responsibility of the ActionBar delegate to route the call to the correct view.Application title will not show as default title on every activity, you can insert different title on every activity. On your activity file bellow the onCreate just paste a single line to set title,
Just change the text "Your Title Here" to your text.