android: any tutorials on creating menus for apps? - android

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

Related

Is it possible to using voice commands for navigating instead of using swipe gesture?

Hi, there
Currently, I'm develop an immersion app to provide a text on screen and user can swipe_right to go another one.
Actually, It adapt from sample immersion pattern called charades(google development site).
My objective is, I want to using voice commands, instead of SWIPE gesture.
for example;
User open the immersion demo, the screen will show first TEXT.
User want to go next TEXT by using voice "GO NEXT".
The screen will show another Text.
Considering this post!
Is there any way to do this?
or any suggestion?
Here, It is my solution. Hope this might helped someone who looking for.
I used Contextual voice commands to provide 'Next', 'Save' and 'Exit' commands for an user. you can go to this document from google dev site to see the ideas of doing this.
I have my layout activity to show some TEXT, so I put this code structure. in my layout activity
//contextual voice command
import com.google.android.glass.view.WindowUtils;
import android.view.Menu;
import android.view.MenuItem;
#Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Pass through to super to setup touch menu.
return super.onCreatePanelMenu(featureId, menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
switch (item.getItemId()) {
case R.id.save_menu_item:
Log.d("Contextual", "go save checks");
break;
case R.id.next_menu_item:
Log.d("Contextual", "go next checks");
break;
case R.id.exit_menu_item:
Log.d("Contextual", "go exit checks");
break;
default:
return true;
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
Don't forget to declare this line getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS); to your onCreate(); before your setContentView().
Next thing, I created 'menu folder' and main.xml inside its to provide my item selection. Like this
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/next_menu_item"
android:title="#string/next">
</item>
<item
android:id="#+id/save_menu_item"
android:title="#string/save_this">
</item>
<item
android:id="#+id/exit_menu_item"
android:title="#string/exit">
</item>
and my strings.xml file.
<resources>
<string name="next">next</string>
<string name="save_this">save</string>
<string name="exit">exit</string>
</resources>
put this line
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
to your AndroidMenifest.xml.
and it works fine for me !

How do I programmatically check if the menu of my activity is showing at a particular moment?

An Android device configuration change (for example "slide the hard keyboard back in") will always call PhoneWindow.onConfigurationChanged(), which in turn, will call reopenMenu(). This will cause the menu of the currently running activity to be reopened, in case it is showing.
I have a lock on my menu implemented in my onPrepareOptionsMenu() override. The user must enter a code each time they want to see the menu. I don't want the user to be asked to enter the code again, while the menu is still up just because of a configuration change. Thus, I would like to know, is there any way I can check if the menu of current foreground activity is already showing? Knowing this, I could bypass asking for the access code if the menu is already up.
My custom workaround implementation is to use my own flag menuShowing, which I set in onPrepareOptionsMenu and reset in onOptionsItemSelected and in onKeyDown if the back button is clicked.
EDIT: It appears a screen orientation configuration change does not trigger this behavior. A hard keyboard slide however, does.
Until someone comes up with a nicer 'one call' answer, here is the custom workaround implementation that I mention in the question, with help from Sam's tips, in case someone needs the same functionality:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (showingMenu) {
// The menu button was clicked or the hard keyboard was
// slid open/closed while the menu was already showing
return true;
}
// Otherwise, either the menu was clicked or openOptionsMenu() was called
if (codeEntered) {
// Code was entered and then openOptionsMenu() was called
showingMenu = true;
// Menu will now be shown
return true;
} else {
// The menu button was clicked, ask for code
askForCode();
// Don't show menu yet
return false;
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
showingMenu = false;
codeEntered = false;
}
private void askForCode() {
codeEntered = getUserInput();
if (codeEntered)
openOptionsMenu();
}
getUserInput() actually occurs with the help of an AlertDialog and an EditText with an attached TextWatcher but the implementation details exceed the scope of this question, unless someone is interested.
In my case it´s
#Override
public void onPanelClosed(int featureId, Menu menu) {
showingMenu = false;
super.onPanelClosed(featureId, menu);
}

Dimming option menu/android

Okay, so I have an option menu for my current app and it is the same with every class. okay, so I would like to know how to dim the options that are included in the menu when the selected class is already selected. For example, I have the Main Home on my option menu. When I am at the Main Home screen...it does appear on the option menu to click. How in the heck do you dim this? I tried looking on the android development page..but had no luck.. yet, I see it on other apps and it is driving me crazy! Surely, I am sure it is easy to mark the code out..but how can you do it! it is driving be bananas!
http://developer.android.com/guide/practices/ui_guidelines/menu_design.html#dim_hide_menu_items
You can override onPrepareOptionsMenu and disable the relevant menu item(s).
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean ok = super.onPrepareOptionsMenu(menu);
if (ok) {
MenuItem item = menu.findItem(id_for_this_screen);
if (item != null) {
item.setEnabled(false);
}
}
return ok;
}

How can I populate a menu using intents?

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;
}

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.

Categories

Resources