Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use Toolbar as an action bar doesn't inflate menu

I'm trying to use Toolbar as an action bar, and I follow the Chris Banes guide:

https://chris.banes.me/2014/10/17/appcompat-v21/

Now following that setup I've an empty Toolbar, it seems that the getMenuInflater().inflate() doesn't work.

empty Toolbar

In my activity I've:

@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 I'm wrong?

like image 916
Giuseppe Avatar asked Mar 17 '23 22:03

Giuseppe


1 Answers

Inorder to use toolbar as actionbar you need to set the attribute windowActionBar as 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>

In manifest.xml under tag use the above theme

android:theme="@style/AppCompatTheme"

I have used toolbar to inflate the menu:

 toolbar.inflateMenu(R.menu.your_toolbar_menu);
like image 163
kunal.c Avatar answered Apr 02 '23 15:04

kunal.c