Icons not showing up in Action bar - android

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.

Related

testing menu_main.xml (Android)

I'm attempting to write unit tests for a very simple app. Here is the menu_main.xml
<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=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/action_refresh" android:title="#string/action_refresh"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
App runs fine, both items show up. MainActivity.xml can clearly find them because
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
works just fine.
I have unit tests that descend from ActivityInstrumentationTestCase2, one of which checks the menu_main.xml.
public void testSettingsMenuItemExists() {
Object view = activity.findViewById(R.id.action_settings);
assertNotNull("Settings menu item does not exist", view);
}
R.id.action_settings obviously exists because the code compiles, but when the tests run, they do not find item. They do find elements from activity_main.xml. I've tried getting the action bar via activity.getActionBar(), it is null. I thought maybe the action bar was owned by the application, not the activity, but Applications don't even have getActionBar().
How do I find menu items so I can test them in unit tests?
I think you have to declare activity class with theme in Manifest file which have action bar default like..
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light"/>
then you can get the action bar and it will not null.

MenuBar sometimes shows icon and sometimes not

I am trying to put an actionBar with two icons (one going to the main activity, and the other one going the previous one)
The thing is that I have this as XML being used as menu:
<item android:id="#+id/Main_App"
android:title="Main"
app:showAsAction="always|withText"
android:orderInCategory="1"
android:icon="#drawable/ic_home"/>
<item android:id="#+id/back_App"
android:title="Back"
app:showAsAction="always|withText"
android:orderInCategory="2"
android:icon="#drawable/ic_back"/>
And the code for using it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Main_App) {
Intent getNewMessageAct = new Intent(this,UpLinksActivity.class);
startActivity(getNewMessageAct);
finish();
return true;
} else if (id == R.id.back_App){
Intent getNewMessageAct = new Intent(this,NewMessageActivity.class);
startActivity(getNewMessageAct);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I always use the same menu, and sometimes icons fits on the ActionBar and sometimes not. I don't get why it is happening.
It's nothing to do about the size because it has space for fitting.
Any idea of why it is happening will be appreciated.
Actually try displaying only icon without text..because ActionBar allocates some fixed space in it for App Title even though it is small..so in small phones it cant display two icons....and even change order in Category to set the same to both the items..
<item android:id="#+id/Main_App"
android:title="Main"
app:showAsAction="always"
android:orderInCategory="100"
android:icon="#drawable/ic_home"/>
<item android:id="#+id/back_App"
android:title="Back"
app:showAsAction="always"
android:orderInCategory="100"
android:icon="#drawable/ic_back"/>
For other ones with the same problem I found a solution:
<item android:id="#+id/Main_App"
android:title="Main"
app:showAsAction="always|withText"
android:showAsAction="always|withText"
android:orderInCategory="1"
android:icon="#drawable/ic_home"
/>
<item android:id="#+id/back_App"
android:title="Back"
app:showAsAction="always|withText"
android:showAsAction="always|withText"
android:orderInCategory="2"
android:icon="#drawable/ic_back"/>
I added "android:showAsAction="always|withText" and it worked. Not really sure why is it working, something related with support libraries I guess. Anyway it gives an XML error but it compiles and works.
the chosen answer didn't work for me.
In my case when I enter in the activity for the first time the menu items don't show in any fragment. If I rotate the device everything works fine.
[I do not have reputation to add a comment!]

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.

Beginning android: Working with the main pop-up menu

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>

Categories

Resources