how to create a common menu for an android application - android

For an android application I need to have a single menu in common that can be displayed on every screen with its structure and functionality written in another class or activity that can be used in all other activities.

Create baseClass Activity. There you can implement the menu structure. After that each activity extends this baseClass activity.

Create a MainActivity which extends ActivityGroup class and all other activity as its sub activities . Use onCreateOptionsMenu(Menu menu) method to addmenu item in MainActivity.
All the other activity can show that menu items automatically.
see this:
public class MainActivity extends ActivityGroup {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("1");
menu.add("2");
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//start other activity here
}
}
Hope this will give you some idea on the issue.

Related

The type SlidingActivity cannot be a superinterface of MainActivity; a superinterface must be an interface

I am trying to use ActionBArSherlock and SlidingMenu in my Application.
I have imported the libraries. My code is:
public class MainActivity extends SherlockFragmentActivity implements SlidingActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.activity_menu);
getSlidingMenu().setBehindOffset(100);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
But I get the following error on SlidingActivity
The type SlidingActivity cannot be a superinterface of MainActivity; a superinterface must be an interface
First of all, SlidingActivity is not an interface. You can not implement it.
Second, you can not extend two class into single class.
Solution
Go into the SlidingMenu library and extend the Activity by SherlockFragmentActivity instead of FragmentActivity.
Then just extend SlidingActivity in your code.

Android load multiple menu layout files possible?

I want to have a superclass activity that defines what basic menu items each activity should have in addition to their own.
Here is my code so far:
public abstract class SuperActivity extends FragmentActivity {
protected List<TextView> textView = new ArrayList<TextView>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentResource());
setTextViews(textView);
}
protected abstract void setTextViews(List<TextView> textViews);
protected abstract int getContentResource();
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menuitemsforallsubclasses, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.basicmenuitem1:
allSubclassActivitiesCanDoThis()
return true;
case R.id.basicmenuitem2:
allSubclassActivitiesCanDoThis2()
return true;
}
}
}
In the subclasses I would then just add something to onCreateOptionsMenu (an additional layout file) and onOptionsItemSelected, is that correct? Just want to make sure I am not on the wrong track here. Would it even be possible to load multiple layout file in the same activity?
FYI: In case you are wondering why I have a TextView array in the activity: In addition to this I want to have all TextViews of each subclass activity in an ArrayList, so that I can add listeners to all of them automatically so I know when something has changed in the activity or to do some other neat stuff.
Is what I am doing possible? Is it possible to load multiple menu layout files?

Android: How to show menu only in specific activities?

I've many activities in my project and one activity for Login but I want that only show menu in all activities except in activity login, because in that menu will be an icon of end session and when press, return to the login activity. And not how, could you help me?
I would recommend that the layout for login does not have the menu and the layout of other activities if the menu has
Better to an base activity which contains your code for menus and then you can extend it your activities instead of activity.
And for login you can extend activity. so all the activities which extend baseactivity will be reflected with menu with out need to write menus code in every activity. and for the will be no menu as it does not extend base activity.
Updated:::
public class BaseActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.menu1:
//your stuff
break;
case R.id.menu2:
//your stuff
break;
}
return true;
}
}

What's the best way to implement a Application wide Options menu on Android?

I'm an Android newbie, and working on an app which needs an Options menu. I have currently implemented the options menu by setting it as the primary activity and Extending it in the main activity.
But since I work in a team, this method doesn't work always with us since we need to extend another activity which is essential.
My Question
How do I implement this Options Menu across the application without Extending the activity in my Main activity?
My Current Setup
I have a MainActivity (This starts first) - MainActivity extends MenuClass
I have the OptionsMenu Class, MenuClass (I want this to be Application wide) - MenuClass extends Activity
I have three other Classes, that extends Activity itself! And these three activities are triggered from the MainActivity and when done, returns to the MainActivity.
If you don't want to, or can't create a base Activity which every other activity then extends - why don't you have a utilities class which has a public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {...} function and a public static boolean onOptionsItemSelected(MenuItem item) {...}?
public class Utils {
public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater ){
//... create default options here
}
public static boolean onOptionsItemSelected(MenuItem item) {
//... see if you want to handle the selected option here, return true if handled
}
}
then from you Activity you can do this:
public class YourActivity extends Activity {
// ...
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ){
Utils.onOptionsItemSelected(menu, inflater);
//... add other options here
}
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled = Utils.onOptionsItemSelected(item);
if (!handled) {
switch(item.getItemId()) {
case R.id.menu_sign_out:
//... deal with option
break;
//.. deal with other options
}
}
return handled;
}
You may want to change the exact implementation of this depending on how you build it in to your app - ie you may not want the utils methods to be static as you may require some state to be maintained in there, but this should work.

Re-using Options menu code

Is there a convenient way of showing the same Options menu options in multiple Activities?
Example: In my app, I display a TV Guide in one of three ways.
Seven day guide (TabActivity with 7 tabs)
All channels 'Now showing' (ListActivity)
All shows today by start time (Activity - could be changed easily to ListActivity)
For the Options menu in the TabActivity, the code is quite simple...
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
menu.clear();
inflater.inflate(R.menu.gv_options_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.view:
...
...
}
}
...but at the moment it seems I need to copy/paste it to the other two Activities which I don't like doing. If I change the Options menu code for one I'll need to do it for the other two also.
The only alternative I can think of is I have a 'helper' class (POJO) to which I could add a method and pass the context into to allow use of the getMenuInflator() method and another method I could pass the result of item.getItemId() into to process with the switch-case.
What is the normal way of having multiple Activities with the same Options menu?
Create a simple separate class with these two methods:
public class MyMenuHandler {
private Activity mActivity;
public MyMenuHandler(Activity activity) {
mActivity = activity;
}
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = mActivity.getMenuInflater();
menu.clear();
inflater.inflate(R.menu.gv_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.view:
...
}
}
}
In your activities override those callback methods and redirect the call to an instance of your MyMenuHandler class:
public class MyActivity1 extends TabActivity {
private MyMenuHandler mMenuHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
...
mMenuHandler = new MyMenuHandler(this);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// you may also add here some items which are specific
// for one activity, not for the others
...
return mMenuHandler.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle selection of your specific items here,
// if none of them has been selected call mMenuHandler method
...
return mMenuHandler.onOptionsItemSelected(item);
}
}
This will let you hold in one place the code which respond to selection of your basic menu items, so there will be no need to worry about copy-pasting it to all activities which are to have the same menu.
One approach is to use inheritance with your Activities. Create a base Activity that implements the options menu methods and then each child Activity will gain that functionality. This is the recommended approach on the Android developer site:
Tip: If your application contains multiple activities and some of them provide the same Options Menu, consider creating an activity that implements nothing except the onCreateOptionsMenu() and onOptionsItemSelected() methods. Then extend this class for each activity that should share the same Options Menu. This way, you have to manage only one set of code for handling menu actions and each descendant class inherits the menu behaviors.
Unfortunately this won't work for you as you are not inheriting from Activity itself but differing subclasses of it, but that is the 'normal' way to do it.
You can encapsulate your action menu in a fragment. In this way you only need to add the fragment in the onCreate menu of your activity.
You need to call setHasOptionsMenu once the fragment is created.
To add the add fragment use a tag instead of a layout id.

Categories

Resources