I am new to this android.In my application when user clicks Menu button ,i want to show 1 option at the bottom of the screen(background should remain as the current screen).Simply i have to say means if user clicks menu button as like pop up window 1 option should come at the
bottom of the screen.And user clicks on that some action should happen..I can t able put some screenshots for this.
----------------------
------------>Consider this is screen
---------------------
------------>here the option should come.
----------------------
Please help me.Thanks in advance.
Menu is generally used to give extra functionality to an application.
To achieve your goal you have to implement menu, which will open when you click menu button of device, like below.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.optionmenu, menu);
return super.onCreateOptionsMenu(menu);
}
R.menu#
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Color" android:title="Color">
<menu>
<item android:id="#+id/RedColor" android:title="Red"/>
<item android:id="#+id/GreenColor" android:title="Green"/>
</menu>
</item>
</menu>
and if you want to do any action on click of option menu, you have to override onOptionsItemSelected to do action on click of menu option, like below.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.RedColor)
{
Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();
}
else if(item.getItemId()==R.id.GreenColor)
{
Toast.makeText(MenuOptionDemoActivity.this, "Green Color Selected", 1000).show();
}
return super.onOptionsItemSelected(item);
}
}
A clear and simple tutorial to implement menu in your app.
That can be done, but the solution is too long for me to post here, so instead follow the link to a tutorial for creating a menu
http://www.androidhive.info/2011/09/how-to-create-android-menus/
Just as a brief overview of the tutorial - You start by overriding the onCreateOptionsMenu method. In this you inflate your menu. Then you override the onItemSelected method, in which you pu a switch to determine which option phase been selected, though in your case it will only be one item. Finally you have to create an XML file which contains all the options for your menu.
Related
In my app I have some activities without menu items, that use the following override:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.appbar_menu_empty, menu);
return true;
}
This works good. If I remove the override, I get the same effect on Android 5.1, i.e. an action bar with no icons.
So the question is: can I drop the override?
The documentation of Activity.onCreateOptionsMenu states:
The default implementation populates the menu with standard system menu items.
What does that mean? Do I need to expect that Android comes up with some buttons I did not explicitly add?
You can remove OncreateOptionsMenu() if you dont want to have menu items.
If you want to add menu items, edit the menu.xml file in resources/menu directory.
From the docs the method is defined in Activity class as below
Initialize the contents of the Activity's standard options menu. You
should place your menu items in to menu.
This is only called once, the first time the options menu is
displayed. To update the menu every time it is displayed, see
onPrepareOptionsMenu(android.view.Menu).
The default implementation populates the menu with standard system
menu items. These are placed in the android.view.Menu.CATEGORY_SYSTEM
group so that they will be correctly ordered with application-defined
menu items. Deriving classes should always call through to the base
implementation.
You can safely hold on to menu (and any items created from it), making
modifications to it as desired, until the next time
onCreateOptionsMenu() is called.
When you add items to the menu, you can implement the Activity's
onOptionsItemSelected(android.view.MenuItem) method to handle them
there.
Parameters:
menu The options menu in which you place your items. Returns:
You must return true for the menu to be displayed; if you return false it will not be shown.
public boolean onCreateOptionsMenu(Menu menu) {
if (mParent != null) {
return mParent.onCreateOptionsMenu(menu);
}
return true;
}
also check this SO thread out onCreateOptionsMenu() calling super
check the code for Activity class here http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/app/Activity.java#Activity.onCreateOptionsMenu%28android.view.Menu%29
see some sample code here where you need to show option in action bar menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_act_add_recipe, menu);
super.onCreateOptionsMenu(menu, inflater);
}
/res/menu/menu_act_add_recipe.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_add_image"
android:icon="#drawable/ic_tab_add_image_white"
android:orderInCategory="100"
android:title="#string/action_preview"
app:showAsAction="always" />
<item
android:id="#+id/action_recipe_preview"
android:icon="#drawable/ic_tab_check_white"
android:orderInCategory="100"
android:title="#string/action_preview"
app:showAsAction="always" />
So my problem is, is that I have a fragment. This fragment is used for creating new objects and editing existing objects.
When I create a new objects, I want to have a save button. And when I edit an object I also want a delete button.
I have two menu layout files. Which are
R.menu.element_actionbar_edit
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_delete"
android:icon="#drawable/ic_action_delete"
android:title="#string/delete"
yourapp:showAsAction="always"/>
<item android:id="#+id/action_save"
android:icon="#drawable/ic_action_done"
android:title="#string/save"
yourapp:showAsAction="always"/>
</menu>
R.menu.element_actionbar_add
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_save"
android:icon="#drawable/ic_action_done"
android:title="#string/save"
yourapp:showAsAction="always"/>
</menu>
Whenever the fragment with all the actions is called, it only onCreateOptionsMenu.
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.element_actionbar_add, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Eventhough I call the R.menu.add layout it still adds two items to the actionbar, before it actually inflates the menu already contains two items.
This fragment is the only place where I actually use a menu right now.
First I had added an if statement in the options create, to check if there was passed an existing object to edit, but in this case it did show the correct layout, but when nothing is passed I should get only the save button?Why does it show the wrong layout, without even specifying it anywhere else?
EDIT:
So it worked!
#Override
public void onPrepareOptionsMenu(Menu menu) {
if(currentObject==null) {
Global.ACTIVITY.getMenuInflater().inflate(R.menu.element_actionbar_add, menu);
}else{
Global.ACTIVITY.getMenuInflater().inflate(R.menu.element_actionbar_edit,menu);
}
super.onPrepareOptionsMenu(menu);
}
At first setting the menu using the onPrepare didn't work either. I had to delete the app from my phone completely, and reinstall it to make it work.
If you want your menus to change dynamically you should not
use onCreateOptionMenu instead use onPrepareOptionMenus
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if(save){
menu.add(0, ADD, 0, "Save");
}
else{
// add rest
}
return super.onPrepareOptionsMenu(menu);
}
this method is called each time you click your menu button and menu.clear() is must since each time you add a menu.
Kindly share your results after implementing.
I am new to developing Android apps.
I would like to add a settings option menu. The requirement for the app is that when you click settings in the option menu the screen will show a sentence Add beep after ___ counts.
How can I do that, please?
Its on the OnCreateOptions and OnOptionsItemSelected, Plus you need to manipulate the menu folder / menu xml also. Try to read about them and instantiating them is as easy as a switch case, as well as for the functions.
You will need a separate activity/fragment to handle Add beep after ___ counts
Here is the flow, read a little bit more on these. You can find lot of examples on each of these:
Create Main Activity with a menu.xml that has a settings action
Create another activity or a dialog fragment with a layout of
<textview> <edittext> <textview>. This is to show the Add beep
after ___ counts. In MainActivity, on menu action "settings", show this fragment.
You can save the count persistently using preferences
Hope this helps.
mainmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:title="#string/settings_label"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
Toast.makeText(context, "Add beep after ___ counts.", duration).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
For more information read this Menu
i am creating an android application which includes maps. I have finished the maps but for the reason that I am new to android programming I would like to ask how to create lets say a menu and when a button is clicked like the map button,then the map would be displayed. For the code that I have got now when i run the application the emulator shows the map,but what do i need to do in order to display a menu first instead of the map.For instance to display some options and when the map button is clicked then the map is shown up. Please help, anyone could give me some advice ?? thank you
Here you need to override onCreateOptionsMenu like:
#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;
}
then you need to override onOptionsItemSelected like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.map:
// write logic here for open map
Intent intent = new Intent(getApplicationContext(), YourMapActivity.class);
startActivity(intent);
}
return true;
}
And XML file is main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/map"
android:title="Map"/>
</menu>
Hi please try the example source code for Sliding Menu for Android.
Please try and let me know. Thanks
Sliding Menu for Android
This topic can look similar to others but I haven't found any usable answer for this case.
Here is what I want : I have a TabActivity with a menu, containing tabs without any menu. When I press the menu button, I want the only existing menu to be displayed. If I only inflate the menu, this is working fine. But if I want to change the content of the menu (change the visibility of items in the onPrepareOptionsMenu(menu) method) or even press the items of the menu, nothing works.
For the onPrepareOptionsMenu(menu), the problem seems to be coming from the menu.findItem(...) method which returns a null Object, and I have a NullPointerException !
In onOptionsItemSelected(item), none of the item IDs will be recognized.
I have noticed with the debugger that the menu Context is the activity of the current tab, so I have moved the menu inside of that activity instead, but without more success.
Last thing, I was using the same menu and very similar code in a previous version of the app, using a single activity (no tabs) and I didn't encounter any problem. When I moved to the TabActivity design it was first working fine (maybe the context of the menu was my TabActivity instead of the Activity of the tab), but it didn't work anymore after minor changes to the Activities of the tabs (nothing related to any menu).
If you think using ActionBar (and the support package) would fix this, I am already planning in moving to it later, but I would like to understand and fix this first.
Here is the code :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem connect1 = menu.findItem(R.id.connect_1);
MenuItem connect2 = menu.findItem(R.id.connect_2);
// Here I do some stuff to prepare the menu, which could be simplified
// like this :
if (device1Connected)
if (!connect1.isVisible()) // here I get the NullPointerException
connect1.setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case R.id.select_device:
// do some stuff
return true;
case R.id.connect_1:
// do some stuff
return true;
case R.id.connect_2:
// do some stuff
return true;
case R.id.disconnect:
// do some stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here is the XML file of the menu, nothing special
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/select_device"
android:icon="#drawable/icon"
android:title="#string/choose_devices"/>
<item android:id="#+id/connect_1"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/connect_1" />
<item android:id="#+id/connect_2"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/connect_2" />
<item android:id="#+id/disconnect"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
android:title="#string/disconnect" />
</menu>
Edit : I have added the following in the onPrepareOptionsMenu(menu) to avoid the NullPointerException. It doesn't fix the real problem but I can now see precisely what happens when I click on a MenuItem
if (connect1 == null || connect2 == null)
return super.onPrepareOptionsMenu(menu);
When I click on the first item, getItemId() returns, for example, 2131165207, and the second item returns 2131165208 (I have checked, they are ids of Views of the second tab !!), but the values it should return to enter the switch/case are respectively 2131165215 and 2131165216, so as I said before, I have a problem with my item ids. I made this test with the menu within the Activity of the first tab, because the mContext value of the menu is always an instance of the current Activity. But even though the Context AND the Activity containing the menu are the same, it still doesn't work.
So ! I have finally figured out what was wrong. It was a building/compiling error. I have been trying before refreshing the project many time, but it was not enough, I had also tried deleting the R.java file (where are compiling the ID numbers from the XML files), but it still didn't work; and I finally remembered about the Project -> Clear... function in Eclipse while working on another project.
And it just worked. So that was nothing there very challenging, but at least I've learnt something valuable today.