Menu not showing up in Activity - Android

Viewed 39899

I have written a code for Menus's in Android but it is now showing up in my activity, here is my code

I am only displaying my relevant code here,

Manifest.xml

<uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >

MainActivity.Java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
        // Some action here

        }
    }
}

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.androidex1.MainActivity" >

   <item android:id="@+id/item1"
       android:title="one" />
    <item android:id="@+id/item2"

        android:title="two" />
    <item android:id="@+id/item3"

        android:title="three" />
    <item android:id="@+id/item4"

        android:title="four" />

</menu>

Steps tried before

Menu Items are not showing on Action Bar

Result:

Tried but still menu's are not appearing

Android options menu not displaying

Result:

Menu's still don't appear.

Android Menu not showing up

Result:

Never added that attribute.

Option Menu does not appear in Android

Result:

super.onCreateOptionsMenu(menu); is also added in the code but still the menu's don't appear.

My problem I would like to see only the menu not the actionbar, what could be the problem ? I am not getting any errors and the main problem is that I am not able to see the menu itself.

16 Answers

YOU HAVE TO extends from AppCompatActivity and not Activity

In my case, I solved it by

android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();

style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">

activity_main.xml

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:background="@color/colorPrimary"/>

Since I am coding fully with kotlin you need to add this into onCreate or onCreateView

setHasOptionsMenu(true)

All you need to do is first attach your toolbar with layout.

 android.support.v7.widget.Toolbar toolbar=findViewById ( R.id.toolbar );
 setSupportActionBar ( toolbar );

and then

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater=getMenuInflater ();
    inflater.inflate ( R.menu.city_menu,menu );
    return true;
}

try changing the emulator. Sometimes the emulator malfunctions to barrage the working.

In my case, I forgot to set support action bar (written in kotlin)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_student_home)
    setSupportActionBar(toolbar) // after adding this line menu shown

have the same problem here, after debuging for hours, i just try to create another layout xml file and update activity to use new layout. and copy paste from old layout, change some ID. dont know why, but it works

If above answer not works, make sure that you are using the same id of toolbar.

layout_app_bar.xml

<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

activiy_main.xml

<include
    android:id="@+id/app_bar"
    layout="@layout/layout_app_bar" />

in java file:

don't call:

Toolbar tb = findViewById(R.id.toolbar);

please call:

Toolbar tb = findViewById(R.id.app_bar);

Step 1: Add this to you toolbar xml file

 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
 app:popupTheme="@style/Theme.AppCompat.Light"

Step 2: Add this to you java code

Toolbar toolbar = findViewById(R.id.order_management_toolBar);setSupportActionBar(toolbar);

That's it.

you call super.onCreateOptionsMenu(menu); before inflate menu on activity so you need to remove this function like this :

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

1.First add toolbar to Activity layout

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@id/toolbar"
        android:layout_width="match_parent"
        app:title="About"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent" />
</com.google.android.material.appbar.AppBarLayout>

2.Add toolbar in Activity

(Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);        

3.Extend AppCompatActivity from your Activity

4.Override onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
Related