Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu not showing up in Activity - Android

Tags:

android

menu

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.

like image 814
GeekDroid Avatar asked Mar 07 '16 14:03

GeekDroid


People also ask

What is menu bar in android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What is the Action bar?

In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users.

What are the different types of menus in Android programming?

There are three types of menus in Android: Popup, Contextual and Options.


2 Answers

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

setHasOptionsMenu(true)
like image 193
Giedrius Šlikas Avatar answered Oct 06 '22 10:10

Giedrius Šlikas


You are inflating the wrong menu file:

getMenuInflater().inflate(R.menu.main, menu);

Your menu file must be named main.xml according to your code.

like image 44
Dmitri Borohhov Avatar answered Oct 06 '22 12:10

Dmitri Borohhov