151 Java Android Program to Demonstrate Adding Menu Items Based on an Intent in Android
Posted by Superadmin on December 09 2015 05:59:51
Here is source code of the Program to Demonstrate Adding Menu Items Based on an Intent in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
Main Activity
package com.example.addingmenuitemsbasedintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
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) {
super.onCreateOptionsMenu(menu);
Intent intent = new Intent(MainActivity.this, Second.class);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(R.id.intent_group, 0, 0, this.getComponentName(),
null, intent, 0, null);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Second
package com.example.addingmenuitemsbasedintent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
main
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<group
android:id="@+id/intent_group"
android:checkableBehavior="single" >
<item
android:id="@+id/delete"
android:title="Delete"/>
<item
android:id="@+id/save"
android:title="Save"/>
</group>
</menu>
second
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>