I’m trying to make my app show the 3-dots menu button up top which will result in a button that when clicked will show the options described in menu.xml(yes I created it in a menu folder with a menu resource file)
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu, menu);
//getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
//I have also tried return true
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.itemOne:
Toast.makeText(this, "settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.itemTwo:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
default:
return super.onOptionsItemSelected(item);
}
}
menu.xml:
I have tried with the parts at the top and without but it didn’t work either way
also it doesnt show the icons
What is shown in the xml, the icons aren’t visible for some reason
what I want it to show when it is being run
what is shown when I run the app and in main activity(don’t mind the settings button above and the logout at the bottom they will be removed)
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<item
android:id="@+id/itemOne"
android:icon="@drawable/baseline_settings_24"
android:title="Settings" />
<item
android:id="@+id/itemTwo"
android:icon="@drawable/baseline_logout_24"
android:title="Logout" />
</menu>
I have honestly tried everything in this world, I have gone through all previous somewhat related threads they all gave answers which I have implemented both in menu.xml and in MainActivity, I just dont understand why it doesnt work or what should I search to make something like this work.
Thanks!!!
Alon Belkin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.