Android common menu options for multiple activities

Android menu options provide user with actions and other options to choose from the action bar of the screen. Some of these actions are common to all the activities for your application, so instead of creating them in each of your activities you can create a BaseActivity that extends the Activity class and does all your menu processing. Then you can extend then base activity class in your application activities to get the same menu options. In case you want a slightly customized menu option for one or more of your activities then override the onPrepareOptionsMenu method to manipulate the menu before its displayed on the screen. Here you can add/remove menu options to the existing menu before being displayed. Here is a simple example doing exactly what we just discussed.

Android common menu options for multiple activities Android menu for main activity Android menu for next activity
If you are not familiar with how to create menu options for Android then please read the following tutorial first
Android options menu, submenu and menu group example

Source for the common menu options - common_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu1" android:alphabeticShortcut="a"
        android:title="Menu No. 1" android:orderInCategory="1" />
    <item android:id="@+id/menu2" android:alphabeticShortcut="b"
        android:title="Menu No. 2" android:orderInCategory="2">
        <menu >
        <group android:id="@+id/group2" android:checkableBehavior="single">
            <item android:id="@+id/submenu1" android:title="SubMenu No. 1" />
            <item android:id="@+id/submenu2" android:title="SubMenu No. 2" />
        </group>   
        </menu>
    </item>
    
</menu>

Source for the main activity layout - activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Main Activity"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <Button
        android:id="@+id/nextActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="false"
        android:layout_below="@id/textView1"
        android:layout_centerHorizontal="true"
        android:text="Go to the next Activity" />

</RelativeLayout>

Source for the next activity layout - activity_next.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Another Activity"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <Button
        android:id="@+id/goBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="false"
        android:layout_below="@id/textView1"
        android:layout_centerHorizontal="true"
        android:text="Go back to previous Activity" />


</RelativeLayout>

Source for the base activity - BaseActivity.java

package com.as400samplecode;


import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class BaseActivity extends Activity{

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.common_menu, menu);
  return true;
 }
 
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

  switch (item.getItemId()) {
  
  case R.id.menu1:
   Toast.makeText(this, "Clicked: Menu No. 1", Toast.LENGTH_SHORT).show();
   return true;
   
  case R.id.submenu1:
            Toast.makeText(this, "Clicked: Menu No. 2 - SubMenu No .1", Toast.LENGTH_SHORT).show();
            return true;
        
  case R.id.submenu2:
            Toast.makeText(this, "Clicked: Menu No. 2 - SubMenu No .2", Toast.LENGTH_SHORT).show();
            return true;  

  default:
   return super.onOptionsItemSelected(item);
  }

 }
 
}

Source for the main activity - MainActivity.java

package com.as400samplecode;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends BaseActivity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button nextActivity = (Button) findViewById(R.id.nextActivity);
        nextActivity.setOnClickListener(this);
        
    }
    
    @Override
 public void onClick(View v) {
  
  switch (v.getId()) {
  
  case R.id.nextActivity:
   Intent nextActivity = new Intent(this,NextActivity.class);
   startActivity(nextActivity);
   break;

  
  }
  
 }
    
}

Source for the next activity - NextActivity.java

package com.as400samplecode;


import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class NextActivity extends BaseActivity implements OnClickListener{

 private static final int MENU3 = 1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        
        Button goBack = (Button) findViewById(R.id.goBack);
        goBack.setOnClickListener(this);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        
        MenuItem menu3 = menu.findItem(MENU3);
        if(menu3 == null){
            menu3 = menu.add(Menu.NONE, MENU3, 3, "Menu No. 3");
        }
        
        return true;
    
    }
    
    @Override
 public boolean onOptionsItemSelected(MenuItem item) {

     super.onOptionsItemSelected(item);
  switch (item.getItemId()) {
  
  case MENU3:
   Toast.makeText(this, "Clicked: Menu No. 3", Toast.LENGTH_SHORT).show();
   return true;
   
  default:
   return super.onOptionsItemSelected(item);
  }

 }
    @Override
    public void onClick(View v) {
     
     switch (v.getId()) {
     
     case R.id.goBack:
      finish();
      break;

     
     }
     
    }
    
    
}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.