Create menu/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
>
<item
android_id="@+id/option1"
android_orderInCategory="100"
app_showAsAction="ifRoom"
android_title="Option 1"/>
<item
android_id="@+id/option2"
android_orderInCategory="100"
app_showAsAction="never"
android_title="Option 2"/>
<item
android_id="@+id/option3"
android_orderInCategory="100"
app_showAsAction="never"
android_title="Option 3...">
<menu >
<group android_id="@+id/group3"
android_checkableBehavior="single">
<item android_id="@+id/submenu3a"
android_title="SubMenu 3A" />
<item android_id="@+id/submenu3b"
android_title="SubMenu 3B" />
</group>
</menu>
</item>
</menu>
MainActivity.java
package com.blogspot.android_er.androidoptionsmenu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.option1:
Toast.makeText(MainActivity.this, "Option 1", Toast.LENGTH_LONG).show();
return true;
case R.id.option2:
Toast.makeText(MainActivity.this, "Option 2", Toast.LENGTH_LONG).show();
return true;
case R.id.submenu3a:
Toast.makeText(MainActivity.this, "Option 3 A", Toast.LENGTH_LONG).show();
return true;
case R.id.submenu3b:
Toast.makeText(MainActivity.this, "Option 3 B", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
0 comments:
Post a Comment