Reputation: 1711
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.
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.
Upvotes: 12
Views: 49657
Reputation: 395
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"
For newer versions of Android:
android:theme="@style/Theme.AppCompat.Light"
android:theme="@style/Theme.AppCompat.Dark"
Upvotes: 18
Reputation: 11
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...
Upvotes: 0
Reputation: 129
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"/>
Upvotes: 8
Reputation: 11
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;
}
Upvotes: 0
Reputation: 1
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;
}
Upvotes: 0
Reputation: 64
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.
Upvotes: 0
Reputation: 4644
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);
Upvotes: 0
Reputation: 1201
Since I am coding fully with kotlin you need to add this into onCreate or onCreateView
setHasOptionsMenu(true)
Upvotes: 4
Reputation: 133
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
Upvotes: 0
Reputation: 1058
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;
}
Upvotes: 2
Reputation: 7557
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
Upvotes: 0
Reputation: 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
Upvotes: -1
Reputation: 57
try changing the emulator. Sometimes the emulator malfunctions to barrage the working.
Upvotes: 0
Reputation: 183
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.
Upvotes: 0
Reputation: 757
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!
Upvotes: 0
Reputation: 1603
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.
Upvotes: 1