Beginning android: Working with the main pop-up menu - android

Apologies for what I am sure is a common question, but after Googling for a while it seems I don't know the proper Android term for what I want and so am stuck.
I would like to know how to work with the main pop-up menu associated with a view...that is, if you are sitting at some activity doing nothing and press the menu key on the phone, how to work with that menu that commonly opens on many apps containing "Settings", "Exit", etc.
I am not sure what this is called, but if someone can point me to the appropriate portion of the SDK, that would be appreciated. Also, if someone knows how to work this menu within the context of the Eclipse ADT plugin, that'd be great, too.
Cheers.

As CaseyB says, it's just called a menu.
For a quick start on using it, you could create a subfolder in the res folder of an eclipse project and call it "menu" add in some xml for the view, and the call a MenuInflator from the onCreateOptionsMenu function.
Ok, that might be a bit confusing, so i've included some sample code that should get you started. This code should make it so when you press the menu button you can select one of two new activities to get loaded.
In Main Activity add:
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// Define whatever other activities you can to load in here or whatever.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.info:
startActivity(new Intent(this, Info.class));
break;
case R.id.logs:
startActivity(new Intent(this, Logs.class));
break;
}
return true;
}
menu.xml file: Link to whatever icon images you want.
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<!-- Copyright © 2012 Tutela Technologies Ltd. -->
<!-- All Rights Reserved. -->
<!-- -->
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/info"
android:icon="#drawable/ic_menu_info_details"
android:title="Info"></item>
<item
android:id="#+id/logs"
android:icon="#drawable/ic_menu_report_image"
android:title="Logs"></item>
</menu>
Then in your AndroidManifest.xml make sure you remember to add in the new activities.
<activity
android:name="com.whatever.Gui.Info"
android:label="#string/appTitle">
</activity>
<activity
android:name="com.whatever.Gui.Logs"
android:label="#string/appTitle">
</activity>
Note: in this example the labels are defined in strings.xml
<string name="appTitle">Your App name</string>
Hope this helps you!
Cheers

It's just called the menu. Here is a tutorial to get you started. Things get a little weird in 3.0+ but once you get the basics down it shouldn't be too hard to pick up.

On versions prior to 3.0, these are menus. Menus are being replaced by the ActionBar in the more recent versions of Android.
About menus: http://developer.android.com/guide/topics/ui/menus.html
Tutorial about menus: http://www.androidhive.info/2011/09/how-to-create-android-menus/

Look at the example:
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.SettingsMenuItem:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In xml: (in /res/menu/menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/SettingsMenuItem"
android:icon="#drawable/settings"
android:title="#string/settings"/>
</menu>

Related

Icons not showing up in Action bar

I am trying to get an action bar within my android app to show up with icons. No matter what I do, I only see the overflow icon with a dropdown, while I want to see the Home and Logout icons next to each other on the action bar menu. I have read through most posts on SO for a solution but am unable to find a solution. Can anyone please help ?
Below is my code :
AndroidManifest.xml
<application
android:name=".application.MySampleApplication"
android:icon="#drawable/letter"
android:label="My Sample"
android:theme="#android:style/Theme.WithActionBar">
Menu_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_logout"
android:icon="#drawable/logout"
android:title="#string/logout"
android:orderInCategory="0"
app:showAsAction="always" />
<item
android:id="#+id/action_home"
android:title="#string/gohome"
android:icon="#drawable/ulogo"
android:orderInCategory="0"
app:showAsAction="always" />
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
logout();
return true;
}
if (id == R.id.action_home) {
goHome();
return true;
}
return super.onOptionsItemSelected(item);
}
How it shows up now
How I want it to show up
MainActivity extends Activity
This means that you are using the native action bar, not the backport offered by appcompat-v7. In that case, replace the app: prefixes in your menu resource with android:, and you will have better luck.
Was googling for a solution and saw someone use it
That is surprising. Your question is the first time I have ever seen anyone use that theme. I didn't even know it existed until I looked it up as part of checking your question. Most likely, you will want to use more modern themes.

Android how can I get ShareActionProvider working on ImageButton click

I am working on a project that requires that the ShareAction menu be opened on an Onclick method and I can not seem to get it to work. First of I have the ShareAction menu working but only in the settings menu and this is; is what I have when I try to apply that to ImageButton Onclick method
// This is inside the Oncreate Method
share= (ImageButton)findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShareActionProvider nn= new ShareActionProvider(Login.this);
Intent ShareIntent=new Intent(Intent.ACTION_SEND);
ShareIntent.setAction(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
nn.setShareIntent(ShareIntent);
}
});
The code above does not work it does not give any errors or nothing. I have that same code working using the settings menu and doing the following
Menu for activity
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Login">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="200" android:icon="#drawable/overflow" app:showAsAction="never" />
<item android:id="#+id/action_share" android:title="#string/action_share" android:orderInCategory="100" app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider"
/>
</menu>
then simply add it to the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
MenuItem shareItem= menu.findItem(R.id.action_share);
ShareActionProvider mShare= (ShareActionProvider)shareItem.getActionProvider();
Intent ShareIntent=new Intent(Intent.ACTION_SEND);
ShareIntent.setAction(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
mShare.setShareIntent(ShareIntent);
return true;
}
What am I missing or doing wrong that is not letting the menu open in Oncreate?
I am working on a project that requires that the ShareAction menu be opened on an Onclick method and I can not seem to get it to work
This does not make much sense. ShareActionProvider is an action bar item. It has its own "button" to allow the user to share content, based on a configured Intent indicating what sort of sharing is to be done (e.g., MIME type).
So, there are three possibilities here, that I can see:
Just use the ShareActionProvider and get rid of this ImageButton.
Get rid of the ShareActionProvider, and do something else to show a list of sharing actions in onClick() of the ImageButton. For example, you could wrap the Intent in Intent.createChooser() and call startActivity() on that Intent from onClick() -- if there are two or more activities that match the Intent, the user will get the chooser "menu".
Fork ShareActionProvider to have it expose some sort of open() method that allows you to activate it from onClick() of your ImageButton. I cannot envision a situation in which this makes any sense (why would clicking a button over here pop open a drop-down menu over there?).

Actionbar compatibility two sets of menu dots

I have been trying to add an action bar to my application.
It is working but not as nice as I would hope. The problem is that I have two sets of three dots for accessing the menu. In the Action bar itself, and along the bottom where my soft keys are. This is the same on a Nexus 4 and a Moto X. Both sets of buttons work. On a Note 8 I have no buttons at all (should I expect the top action bar ones?), but the (physical) menu key does what it is supposed to do.
Extracts from Manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
login_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/login"
android:title="#string/menu_login"
android:icon="#android:drawable/ic_lock_idle_lock"
app:showAsAction="never"/>
</menu>
Extracts from Login.java
public class Login extends ActionBarActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.login:
handleLogin();
return true;
}
return super.onOptionsItemSelected(item);
}
This is how it looks, any assistance appreciated.
As per the Say Goodbye to the Menu Button blog post, you need to add a targetSdkVersion of 14 or higher:
<uses-sdk android:minSdkVersion="10"
android:targetSdkVersion="19" />
Note it is a good practice to always set your target SDK version to the highest version you have tested your app on as it is the target SDK version that sets up compatibility modes such as what you are seeing.

Search View uses app icon instead of logo

Android documents explain that app logo is used everywhere when it is defined. But when the search view expands, app icon is used instead of app logo and I can't find a way to show app logo when search view is in expanded state.
Here are the relevant parts.
Manifest file
<application
android:name="MyApp"
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:logo="#drawable/app_logo"
android:theme="#style/Theme.MyTheme" >
searchable.xml (setting icon doesn't change anything)
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:icon="#drawable/app_logo"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="#drawable/ic_search"
android:showAsAction="always|collapseActionView" />
Activity Code
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.menu_search).setActionView(searchView);
NOTE: I use ABS SearchView but same thing happens when I switch to default SearchView.
EDIT : I added some screenshots for clarity.
Here I used the star image for app logo, and android image for app icon.
The first screenshot is the default view of activity.
The second one is the view when I click search button.
It shows the android image while I expect it to be the star image.
Set the logo drawable as your icon. Via theme attribute
<item name="android:icon">#drawable/ab_logo</item>
or in code
getSupportActionBar().setIcon(R.drawable.ab_logo);
You're using a completely different SearchView you declared in menu.xml.
With this code you create new SearchView, which is not what you want.
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
So try creating menu like this.
this.getSupportMenuInflater().inflate(R.menu.menu, menu);
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
final MenuItem searchItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(info);
And for the ActionBar, instead of setDisplayUseLogoEnabled(true) and others, try set all options at once
final ActionBar bar = getSupportActionBar();
bar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE |
ActionBar.DISPLAY_USE_LOGO |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_HOME_AS_UP);
This is the setup I have in my current project and never experienced any problems like these.
I had the same problem and I solved it using the following code:
menu.findItem(R.id.menu_search).setOnActionExpandListener(this);
// Your Activity/Fragment must implement the MenuItem.OnActionExpandListener interface
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
getSherlockActivity().getSupportActionBar().setIcon(R.drawable.uvweb_logo);
return true;
}
Let me know how this works for you.
I achieved the solution with this simple logic:
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_search:
getActionBar().setIcon(R.drawable.your_logo);
return true;
}
return super.onOptionsItemSelected(item);
}
Just remove collapseActionView from your menu Xml.
To replace the icon with a logo, specify your application logo in the manifest file with the android:logo attribute, then call setDisplayUseLogoEnabled(true) in your activity.
refer this document1
document2
And please see this answer for more details
The only thing you need to have set is this (in onCreate of activity):
getSupportActionBar().setIcon(R.drawable.logo);
...not there is also a setLogo.
Regardless to if you have the icon in the manifest set to something different, this will tell the activity to use a different image during runtime.
I tested this with the following menu item and it works:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_hint"
android:orderInCategory="1"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always|collapseActionView"/>
</menu>
Inflated like normal in onCreateOptionsMenu:
getSupportMenuInflater().inflate(R.menu.search_menu, menu);
I had the same problem, (for ABS) I had:
ActionBar actionBar = ((SherlockFragmentActivity) activity).getSupportActionBar();
actionBar.setLogo(R.drawable.logo);
And I just added:
actionBar.setIcon(R.drawable.logo);
And the issue is gone.
I found the best way to solve this problem was to add the SearchView programatically to the menu like this:-
SearchView searchView = new SearchView(getActionBar().getThemedContext());
menu.add("Search")
.setIcon(R.drawable.ic_action_search)
.setActionView(searchView)
.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
getActionBar().setIcon(R.drawable.ic_action_willyweather);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
})
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
And then use the onActionExpandListener to change the icon/logo to the correct value when then SearchView is expanded.
This solution may also work if you define the SearchView in menu.xml, but you'd then still need to setup the onActionExpandListener() in code to do the same as the above example.
Andy.

Soft menu button not appearing in ICS

My activity has a menu that inflates on my emulator when I use its hardware "Menu" button, however when I run the app on my actual phone (Galaxy Nexus with 4.0.1) I cant work out a way to make the "3 dot" style menu button appear as it does in some apps. The way that I've implemented my menu is:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.wcoptions, menu);
return (super.onCreateOptionsMenu(menu));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.ooptions:
// open preferences activity
startActivity(new Intent(this, WorkCalcPreferenceActivity.class));
break;
case R.id.oquit:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
Also in my manifest file I have the minSDK set as 8 and the targetSDK set as 15.
This is my first app and Im still learning how to implement all these things so any help would be greatly appreciated :)
Edit: Here is menu.wcoptions
<item
android:id="#+id/ooptions"
android:icon="#drawable/ic_menu_settings"
android:title="Options"/>
<item
android:id="#+id/oquit"
android:icon="#drawable/ic_menu_exit"
android:title="Quit"/>
If you target Honeycomb or above you won't get the menu button. The menu button is only there for legacy purposes, and you should instead make use of the action bar or some other means of bringing up the menu.

Categories

Resources