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.
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.
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.
There are three types of menus in Android: Popup, Contextual and Options.
Since I am coding fully with kotlin you need to add this into onCreate or onCreateView
setHasOptionsMenu(true)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With