How can I populate a menu using intents? - android

How can I populate a menu using intents? I didn't understand that thing.
Or is there any better way for that?
Update:
Suppose I have an application that need to resize the image,and there are many other applications that have a capability of resizing the image. How can I show the list of applications on a menu in my application so that when clicking on a particular option it will invoke the intent associated with that application. Simply saying I could resize the image in my application with out bothering about how that will get done.

Here you have an example, using the onOptionsItemSelected that appears with the menu button you control what the menu does:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case HISTORY_ID: {
AlertDialog historyAlert = historyManager.buildAlert();
historyAlert.show();
break;
}
case SETTINGS_ID: {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.setClassName(this, PreferencesActivity.class.getName());
startActivity(intent);
break;
}
}
But we create the menu with this code:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, SETTINGS_ID, 0, R.string.menu_settings)
.setIcon(android.R.drawable.ic_menu_preferences);
menu.add(0, HELP_ID, 0, R.string.menu_help)
.setIcon(android.R.drawable.ic_menu_help);
menu.add(0, ABOUT_ID, 0, R.string.menu_about)
.setIcon(android.R.drawable.ic_menu_info_details);
return true;
}

Related

Using icon in top left of ActionBarSherlock to navigate

Using the developers guide found here, I am trying to make my icon navigate back to my home screen. I currently have a button which does this, and have copy and pasted the code in the onOptionsItemSelected() method. However tapping the icon never does anything. Is this a difference in ActionBar and ActionBarSherlock?
This is the code given as an example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the code that I am using:
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case R.id.mainTopBluetoothState:
Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
return true;
case R.id.mainTopAppState:
Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
return true;
case android.R.id.home:
Log.i( "In Home", "In Home" );
killToasts();
dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK ));
finish();
return true;
}
return super.onOptionsItemSelected( item );
}
When I tap the icon, nothing happens. The Log call in the code isn't ever shown in my LogCat either.
You are probably not enabling the ABS Activity logo to be clickable. Add this in onCreate()
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Also, if you haven't done so already, read Implementing Ancestral Navigation so you navigate up properly (ignore their use of getActionBar(), they're not using ABS, and that's the actual Android API Action Bar method).

how to reload menu android in the same activity?

I have a menu in my app android. When I click on add favorites, I need to reload the menu options, making it appear del favorites in the options and not appear add favorites.
I don't want use reload activity because of the back button.
My code:
public boolean onCreateOptionsMenu(Menu menu) {
try
{
MenuItem menuInicio = menu.add(INICIO, INICIO, 0, "InĂ­cio");
menuInicio.setIcon(android.R.drawable.ic_menu_edit);
MenuItem menuBusca = menu.add(BUSCA, BUSCA, 0, "Buscar");
menuBusca.setIcon(android.R.drawable.ic_menu_search);
SubMenu menuFavoritos = menu.addSubMenu(FAVORITOS, FAVORITOS, 0, "Favoritos");
if(!phytoterapicContent.getPhytoterapicItem().getIsFav())
menuFavoritos.add(FAVORITOS, ADD_FAV, 0, "Adicionar aos Favoritos");
else
menuFavoritos.add(FAVORITOS, DEL_FAV, 1, "Remover dos Favoritos");
menuFavoritos.add(FAVORITOS, LIST_FAV, 2, "Listar Favoritos");
menuFavoritos.setIcon(android.R.drawable.star_off);
}
catch (Exception e) {
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case INICIO:
Intent it = new Intent(ShowPhytoterapicActivity.this, HomeActivity.class);
startActivity(it);
break;
case BUSCA:
Intent it3 = new Intent(ShowPhytoterapicActivity.this, ShowSearchParametersActivity.class);
startActivity(it3);
break;
case ADD_FAV:
try {
Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao();
phytoterapicContent.getPhytoterapicItem().setIsFav(true);
phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem());
Toast.makeText(ShowPhytoterapicActivity.this, "Adicionado aos Favoritos", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case DEL_FAV:
try {
Dao<PhytoterapicItem, Integer> phytoterapicItemDao = getHelper().getPhytoterapicItemDao();
phytoterapicContent.getPhytoterapicItem().setIsFav(false);
phytoterapicItemDao.update(phytoterapicContent.getPhytoterapicItem());
Toast.makeText(ShowPhytoterapicActivity.this, "Removido dos Favoritos", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case LIST_FAV:
Intent it5 = new Intent(ShowPhytoterapicActivity.this, ShowFavoritesActivity.class);
startActivity(it5);
break;
}
return true;
}
Thanks!
use onPrepareOptionsMenu
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if(isChangedStat) {
menu.add(0, MENUITEM, 0, "True");
} else {
menu.add(0, MENUITEM, 0, "False");
}
return super.onPrepareOptionsMenu(menu);
}
Please note two points
1- If possible just enable or disable menu item or looks possible in your case can change the title of same menu will work because menu.clear(); may need over attention while handling
2- as per link provided by Atlos
On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).
On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().
Here is a relevant link: http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu
It depends what version of Android you are targeting. Not sure if you have read this part yet, but any other advice I have would be this verbatim.

Android - checkable buttons on options menu

Can anyone point in the direction of any tutorials that show how to create an options menu with clicakble checks like in the picture below:
alt text http://img291.imageshack.us/img291/1221/deviceit.png
I have tried as follows:
/** Menu creation and setup **/
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Speaker").setCheckable(true);
menu.add(0, 2, 0, "Mute").setCheckable(true);
return result;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
if(audioManager.isSpeakerphoneOn()==false){
audioManager.setSpeakerphoneOn(true);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
}else{
audioManager.setSpeakerphoneOn(false);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
}
return true;
case 2:
if(audioManager.isMicrophoneMute())
audioManager.setMicrophoneMute(false);
else
audioManager.setMicrophoneMute(true);
return true;
}
return false;
}
But this doesn't work it only gives me text on the buttons on the options menu
EDIT: I have added the following onPrepareOptionsMenu method:
public boolean onPrepareOptionsMenu(Menu menu){
boolean result = super.onPrepareOptionsMenu(menu);
if(audioManager.isSpeakerphoneOn())
menu.findItem(1).setChecked(true);
else
menu.findItem(1).setChecked(false);
if(audioManager.isMicrophoneMute())
menu.findItem(2).setChecked(true);
else
menu.findItem(2).setChecked(false);
return result;
}
However I get the same outcome just text and no check light as in the picture above
If you want to change dynamically the state of your Option Menu, you need to use onPrepareMenu(). In this method, you can do dynamic checks and update anything you want.
Good luck!!
documentation
After some digging, this look like a custom view. I think your picture comes from this code.
This is an old question, but I had the same problem and searched a lot to find such an optionsMenu shown above. I found a tutorial on http://www.codeproject.com and modified it a little bit. Maybe it is not a profi-programmers-code, but it works for me. See my modifications at my (poor arranged) web page on google sites (I got an little tutorial too on this site):
https://sites.google.com/site/opiatefuchs/android-code-examples
This code is original from wjfrancis on the Code Project web page (many props):
http://www.codeproject.com/Articles/173121/Android-Menus-My-Way
I just modified it and would be glad if somebody got any ideas to improve this code. But for now, it works.

option item selection in Dialog menu

I have been running into some troubles recently and I think I need your help :).
I am currently trying to show a menu on top of a dialog, I know that it could be far easier to launch a new activity yet doing so would compell me to store/pass a lot of data.
I managed to show an optionmenu by writing a custom dialog and rewriting the oncreateOptionMenu method.
My problem is I can't get any listener to these button, I tried to rewrite the onoptionitemselectedmethod but nothing happens.
Ps: my dialog is nearly full screen so i can't see the activity dialog (i didn't find any put on top method)
I would be glad to try any solution you could provide.
Thanks a lot
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,0,Menu.NONE,c.getString(R.string.home));
menu.add(Menu.NONE,4,Menu.NONE,c.getString(R.string.report));
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==0){
getOwnerActivity().startActivity(new Intent(c,Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
else
if(l>1)
getOwnerActivity().startActivity(new Intent(c,report.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
else
dismiss();
return true;
}
Maybe this little snippet out of my app helps you:
private static final int REFRESH_ID = Menu.FIRST + 1;
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, REFRESH_ID, 0, R.string.menu_refresh).setIcon(R.drawable.and_refresh);
return result;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case REFRESH_ID: {
// Do whatever you want here!
return true;
}
}
return super.onMenuItemSelected(featureId, item);
}
As you can see, I've got constants for my menu-items. Those items get the Menu.First + n number as integer. For every item, I count it up. Easier, then change it everytime ;)
And in the onMenuItemSelected you can switch those constants easily.
Hope that helps!

android: any tutorials on creating menus for apps?

I need a very simple menu which probably contains only one or two items: settings/options, where pressing one of them should show some customer defined parameters (is it called dialog), e.g., number of results shown. Is there any good tutorial on creating such kind of menus? I've looked at the "notepad" example in android, it doesn't really help.
Depending on what you're asking for, these are either "Options Menus" or "Context Menus", and creating them is very easy. Here's a link to the page on the Developers' Website explaining how to do menus.
Here's a basic example of code for options menus, adapted from my game:
public boolean onCreateOptionsMenu(Menu menu){
// Define your menu, giving each button a unique identifier numbers
// (MENU_PAUSE, etc)
// This is called only once, the first time the menu button is clicked
menu.add(0, MENU_PAUSE, 0, "Pause").setIcon(android.R.drawable.ic_media_pause);
menu.add(0, MENU_RESUME, 0, "Resume").setIcon(android.R.drawable.ic_media_play);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu){
// This is called every time the menu button is pressed. In my game, I
// use this to show or hide the pause/resume buttons depending on the
// current state
}
public boolean onOptionsItemSelected(MenuItem item){
// and this is self explanatory
boolean handled = false;
switch (item.getItemId()){
case MENU_PAUSE:
pauseGame();
handled = true;
break;
case MENU_RESUME:
resumeGame();
handled = true;
break;
}
return handled;
}
Edit: See the comments for some details on AlertDialogs

Categories

Resources