Use the toolbar as the action bar doesn't bloat the menu
I am trying to use a toolbar as an action bar and I am following Chris Banes' guide:
https://chris.banes.me/2014/10/17/appcompat-v21/
Now after this installation I have an empty toolbar, it seems like getMenuInflater (). inflate () doesn't work.
In my activity I:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menuhome, menu);
[...]
and in onCreate ():
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
And this is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:allmytv="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/linearlayoutmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
xmlns:allmytv="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
allmytv:pstsIndicatorColor="#f57c1d" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
Where am I going wrong?
source to share
This was a very specific problem. The problem was that I have a splash screen with the setting:
setContentView(R.layout.splashhome);
The toolbar was empty because I was calling setSupportActionBar (toolbar) when the splash screen was active.
Moving setSupportActionBar (toolbar) to the correct location fixed!
source to share
To use the toolbar as an action bar, you need to set the windowActionBar attribute to false.
Include the following in styles.xml
<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="colorPrimary">#4285F6</item>
</style>
The manifest.xml tag below the tag uses the above theme
android:theme="@style/AppCompatTheme"
I used the toolbar to inflate the menu:
toolbar.inflateMenu(R.menu.your_toolbar_menu);
source to share