11

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.

Community
  • 1
  • 1
oldcode
  • 1,669
  • 3
  • 22
  • 41
  • ok, i suggest to use Android Studio! You see my post? I edit it... – Dario Mar 07 '16 at 16:06
  • @Dario Tried the same in Studio but don't understand what is happening ? Still not able to see the simple menu – oldcode Mar 07 '16 at 16:14
  • Can you post some image of your app? Add more information so we can understand what is wrong! I simple create a new project, create a menu.xml with your code, add the menu to the activity and if i press menu button in my phone Galaxy S3 i see the 3 choice of the menu. Same for the AVD with nexus 7 in android studio. I suggest to use android studio updated with the last version of the SDK and try again. – Dario Mar 07 '16 at 16:17
  • Post an image of your result and an image of what you expect if you can! – Dario Mar 07 '16 at 16:20

17 Answers17

16

It is unclear for me, do you see your action bar and don't see menu in it, or you don't see action bar at all? if it is the latest, try applying style which supports action bar in your <application> tag in manifest. For example:

android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • I don't see the Actionbar infact I have not added any Actionbar to my activity – oldcode Mar 07 '16 at 14:31
  • 2
    Because you extend Activity in your Java class. You can't use Theme......DarkActionBar if you extend Activity. Now ActionBarActivity it's deprecated, use AppCompatActivity and use Toolbar that is the new ActionBar – Dario Mar 07 '16 at 14:35
  • I would like to extend Activity not AppCompatActivity. What would be the solution to this ? – oldcode Mar 07 '16 at 14:37
  • try adding in onCreate requestWindowFeature(Window.FEATURE_ACTION_BAR); – Anastasiia Chervinska Mar 07 '16 at 14:46
  • Where should I call this function ? – oldcode Mar 07 '16 at 14:49
  • in onCreate - like this `protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.activity_main); }` – Anastasiia Chervinska Mar 07 '16 at 14:55
6

YOU HAVE TO extends from AppCompatActivity and not Activity

Bano Adam
  • 77
  • 1
  • 1
  • 1
    You could post some code as an example on how to do what you suggest to improve your answer. – rsz Feb 04 '18 at 20:40
  • It's pretty self explanatory. Instead of: public class MainActivity extends Activity { ... } Do this: public class MainActivity extends AppCompatActivity { ... } – Scott Siddall Dec 16 '20 at 19:14
6

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"/>
Community
  • 1
  • 1
Efhem
  • 101
  • 1
  • 3
4

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

setHasOptionsMenu(true)
Giedrius Šlikas
  • 1,073
  • 12
  • 12
2

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;
}
Harpreet Singh
  • 878
  • 1
  • 7
  • 5
1

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.

Dmitri Borohhov
  • 1,513
  • 2
  • 17
  • 33
0

I tried your code and in my phone all works! The only different thinks is that

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                //DoSomething();
                Toast.makeText(getApplicationContext(), "Hello 1", Toast.LENGTH_LONG).show();
                return true;
            case R.id.item2:
                //DoSomething2();
                Toast.makeText(getApplicationContext(), "Hello 2", Toast.LENGTH_LONG).show();
                return true;

            case R.id.item3:
                //DoSomething3();
                Toast.makeText(getApplicationContext(), "Hello 3", Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Are you sure that the Directory and the name of the menu it's right?

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

Be sure that the directory is menu and the file menu is main.xml When it is autogenerated Android Studio call it main_activity_menu.xml, so be sure that the name in the inflater are right because i make a new test app with your code and i see the menu!

Read this Google.developer guide if you want add an ActionBar with menu items like icons

Another Menus guide from google.developer without actionbar if don't need Happy coding!

Dario
  • 732
  • 7
  • 30
  • Why is the usual method not working ? Why should we extend AppCompatActivity ? I would still like to extend Activity. Sorry this does not answer my question. – oldcode Mar 07 '16 at 14:35
  • You want an ActionBar to your Activity right? If you extend only Activity you can't have an ActionBar. For get what you want follow the code that i post, or change your question because it's wrong maybe. – Dario Mar 07 '16 at 14:38
  • [See this](http://www.javatpoint.com/android-option-menu-example), how come one achieved menu without extending AppCompat Activity ? – oldcode Mar 07 '16 at 14:41
  • So you want only a menu. Ok, edit your post it's not clear. You get some errors? Post the log If you touch the menu button on the phone what happen??? nothing is shown? Anyway try to extend AppcompatActivity not only Activity it's don't destroy your phone don't worry! – Dario Mar 07 '16 at 14:45
  • Please see the "My problem" section in my question, I have edited it. – oldcode Mar 07 '16 at 14:48
  • All people here had understand that you need an ActionBar here, edit better your post! Now i edit the code with a simple menu – Dario Mar 07 '16 at 14:56
  • The following helps `getMenuInflater().inflate(R.menu.yourxmlfilename, menu)` – yas Oct 11 '17 at 21:44
0

I've had the same problem. It was a styling

In my project created in android studio, i have style.xml file: There is a line.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

And in my manifest.xml (you are not showing your full manifest file). One of the activities (where i wanted to show menu) has following line set:

<activity
    android:name=".UsersListActivity"
    android:label="@string/title_activity_users_list"
    android:theme="@style/AppTheme.NoActionBar">
</activity>

So when i've changed AppTheme.NoActionBar to AppTheme.AppTheme and now it works:

<activity
    android:name=".UsersListActivity"
    android:label="@string/title_activity_users_list"
    android:theme="@style/AppTheme.AppTheme">
</activity>

and now it works.

Remiwaw
  • 163
  • 2
  • 13
0

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

  • Can you give more details since this answer does not look so promising – Ibo Oct 23 '17 at 00:10
  • sometimes the emulator that you have created malfunctions and this leads to the non proper implementation of your created APP. Happened to me once all I did was to change the emulator before re-running my APP. it worked. – Justin Joseph Oct 24 '17 at 23:50
0

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
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
0

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

indrakula
  • 113
  • 1
  • 5
0

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);
Ahamadullah Saikat
  • 4,437
  • 42
  • 39
0

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.

Arup
  • 54
  • 1
  • 8
0

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;
    }
malek
  • 1
0

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;
}
chornge
  • 2,021
  • 3
  • 28
  • 37
srinivas
  • 11
  • 5
0

some may be facing problem like their menu with items is not visible or showing on emulator but showing in layout validation to fix it go to app>manifests In AndroidManifests .xml add following line android:theme="@style/Theme.AppCompat.Light.DarkActionBar" insted of previous android:theme ..line

this solved my problem...

Gayatri
  • 11
  • 2
-1

I have the same problem .... At tge end i tryied all the codes and turns out it only worked when I changed the extend in the Java class... Try using the default one AppCompatActivity

N.M
  • 1
  • This should be reworded to a proper answer. In it's current state this answer looks more like a comment. – Filnor Apr 03 '18 at 07:35