Step 1. Create an android project in Android Studio and fill all require fields.
Step 2. Now create menu folder into res option.
Step 4. Now go to Main_Activity.java class and type the code.
package com.demo.optionmenu;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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.option_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
System.out.println("=====> menu item:" + itemId + " selected");
switch (itemId) {
case R.id.main_menu1:
System.out.println("Menu1 Selected!");
break;
case R.id.main_menu2:
System.out.println("Menu2 Selected!");
break;
}
return super.onOptionsItemSelected(item);
}
}
Step 5. Now run this app in your Emulator.
0 Comments