Action bar not fitting all the items - android

Here is my code for my actionbar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/actionBarNew"
android:showAsAction="always"
android:scaleType="fitXY"
android:title="new" />
<item android:id="#+id/actionBarSave"
android:showAsAction="always"
android:scaleType="fitXY"
android:title="save" />
<item android:id="#+id/actionBarLoad"
android:showAsAction="always"
android:scaleType="fitXY"
android:title="load" />
<item android:id="#+id/actionBarDelete"
android:showAsAction="always"
android:scaleType="fitXY"
android:title="delete" />
</menu>
Then in my onCreate() method, I add the switch widget like so:
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END));
actionBar.setTitle(title);
actionBarSwitch.setText(sfx);
actionBarSwitch.setTextColor(res.getColor(R.color.white));
actionBarSwitch.setChecked(soundOn);
actionBarSwitch.setOnCheckedChangeListener(this);
Here is what it looks like at the moment:
As you can see, the switch widget on the left is covered up. It is supposed to say "SFX" on the left of it but it doesnt fit. Is there any way to keep the icon and all the other action bar icons visible? Maybe scale to fit or something?
Any help is appreciated, thanks.

Here is my code for my actionbar
I do not believe that android:scaleType is a recognized attribute for a MenuItem.
Is there any way to keep the icon and all the other action bar icons visible?
Use the split action bar, to move your action bar items (NEW, etc.) to the bottom of the screen on narrow devices.
Maybe scale to fit or something?
While you are welcome to attempt to use view properties to scale the size of your Switch, there is no guarantee that there is enough room to have a usable Switch when you're done. You do not have control over scaling of the regular action bar items.

IMHO this is bit of an abuse of the ActionBar. If you definitely want to fit everything in there, you might try mimicking the ActionBar by adding a LinearLayout at the top of your activity with a background color. You should be able to use the LayoutWeight parameters to get the widgets to fit then.

Related

Action bar (Sherlock) menu items cutting off built-in Spinner

My action bar menu items are cutting off the width of the action bar Spinner so that the Spinner's text gets truncated. I've set all my menu items to use "ifRoom" but unfortunately they interpret the original Spinner width as available room.
Here's the truncation when I have three menu items (yes, I know the third icon looks identical to the second):
Here's what I get when I comment out the third menu item. Ultimately, I want something that looks like this, but with an overflow icon showing instead of the search icon:
I populate the Spinner using a custom adapter (extending from BaseAdapter and implementing SpinnerAdapter).
Here's my menu XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/ab_refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/ab_refresh"
android:icon="#drawable/navigation_refresh"/>
<item
android:id="#+id/ab_search"
android:orderInCategory="2"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/ab_search"
android:icon="#drawable/action_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>
<item
android:id="#+id/ab_toggle"
android:orderInCategory="3"
android:showAsAction="ifRoom"
android:title="#string/ab_latest"
android:icon="#drawable/action_search"/>
</menu>
And here's my ab_dropdown XML (used in getView of my custom adapter):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="fill_horizontal">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp" />
</RelativeLayout>
What I've tried:
Setting a minWidth of 570dp for the RelativeLayout and TextView of ab_dropdown. No effect regardless of whether I set it in XML or the getView method of my custom adapter.
Hardcoding the layout_width of RelativeLayout and TextView to 570dp. Again, no effect in XML or code.
I WOULD attempt to find the action bar Spinner programmatically so as to set its width, but when I tried to do this for a different reason, I couldn't.
Any ideas?
Personally, I'd switch to a navigation drawer and dump the drop-down list navigation.
That being said, it sounds like you need two versions of your menu resource. Have the third item be never in the toolbar (always in overflow) by default, and have it be ifRoom on larger screens (e.g., res/menu-sw600dp/).
You could also experiment with AutoScaleTextView for your drop-down list navigation labels.
The navigation portion of the action bar is deemed least important, which is why ifRoom considers there to be room.

Make menu items fill height of action bar

I have an app with an action bar. I need the menu items to fill the height of the action bar. I have tried styling the action bar with android:padding="0dp" and settings android:layout_height="fill_parent" on the action views to no avail.
This picture shows that the browse subjects actionview for instance does not fill the actionbar.
Here is a shortened version of my menu declaration.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_more"
android:title="More"
android:showAsAction="always"
android:actionProviderClass="pacakge.action_providers.MoreProvider"
/>
</menu>
The action provider class inflates and returns a view that is inserted into the ActionBar.
Any help would be greatly appreciated! Thanks
Ended up just hard coding the button height to 50dp. Not a very elegant solution but it works.
Hmm....looks like you need to use bigger icons.
Also, in the layout for the buttons, use android:layout_height as "fill_parent".
And as a last tip, use android:showAsAction="ifRoom"
Try to change menu item layout to RelavtiveLayout

Have the split ActionBar display twice as many icons

I'd like to have an app layout as the one they used for Gmail. They have the actionbar that has icons (on my device it has the app logo for up navigation, and a custom view for selecting priority inbox/inbox/etc), but there is also some kind of split ActionBar because at the bottom I have several icons (compose, search, labels, etc).
How could I do this? I have implemented the following ActionBar through this XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_gps"
android:icon="#drawable/icone_gps_continu"
android:showAsAction="always"
android:title="#string/menu_gps"/>
<item
android:id="#+id/menu_sort"
android:icon="#drawable/icone_liste_chron"
android:showAsAction="always"
android:title="#string/menu_liste"/>
<item
android:id="#+id/menu_stats"
android:icon="#drawable/icone_statistiques"
android:showAsAction="always"
android:title="#string/menu_stats"/>
<item
android:id="#+id/menu_save"
android:icon="#drawable/icone_terminersortie"
android:showAsAction="always"
android:title="#string/menu_save"/>
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/menu_photo"
android:icon="#drawable/icone_enregistrerphoto"
android:showAsAction="always"
android:title="#string/menu_photo">
</item>
<item
android:id="#+id/menu_sound"
android:icon="#drawable/icone_enregistrerson"
android:showAsAction="always"
android:title="#string/menu_sound">
</item>
<item
android:id="#+id/menu_settings"
android:icon="#drawable/icone_parametres"
android:showAsAction="always"
android:title="#string/menu_settings">
</item>
</menu>
And I have of course enabled the split ActionBar by adding android:uiOptions="splitActionBarWhenNarrow" to my <application> tag in the manifest.
What I get though is an empty "top" ActionBar; and the bottom one is displaying only three icons. I thought I could display like 3-4 icons at the top and 5 at the bottom in portait mode, and all of them at the top in landscape mode.
Note: even if I'm testing on ICS with my Galaxy Nexus, I'm using ActionBarSherlock in order to provide backward compatibility.
Looks like I posted too fast, it seems it's not possible.
Duplicates:
How to Place ActionBar Items in Main ActionBar and Bottom Bar
Android Split Action Bar with Action Items on the top and bottom?
Apparently this is not possible. However they did it in gmail/gplus, maybe using a custom view for the bottom part... Still expecting a better answer to this!
Reto Meier's word on this:
I think the problem with splitting the actions between the top and bottom is that it would make it more difficult to perform actions. Actions should be the most important things to do on an Activity -- splitting them between the top and bottom of the screen means users need to look in two places rather than one.
I was also wondering how these apps had icons in both bars, so I decompiled two of them.
What I found is that these two apps didn't implement the ActionBar at all and use custom layouts for both bars. I'm not sure if I can name these apps here, but they are well known apps with 10M+ downloads.
I strongly suggest to go the same way if you don't like the default ActionBar (which have very little room for customization) and create custom layouts instead (not necessary for both bars, it depends on your needs).
It's quite easy to reproduce the split action bar layout. Its height is always 48dp whatever the device is, so a simple LinearLayout or RelativeLayout with a fixed height and some transparent image buttons work very vell. In only 15 minutes you get a fully customizable split action bar.

Drop shadow on Action Bar and Custom Camera View

Currently, I have the following application, which consists of a ActionBar and a Custom View. It looks like this
I try to achieve drop shadow effect on
Action bar (Use library from ActionBarSherlock)
Custom camera view
By using "9 patch" technique, I am able to drop shadow on the action bar, but not the custom view.
Here is the technique I am using. I specific windowContentOverlay to a "9 patch" image. According to documentation, here is what windowContentOverlay used for.
This Drawable is overlaid over the foreground of the Window's content
area, usually to place a shadow below the title.
I expect both action bar and custom view are categorized as Drawable. By having the following style across entire activity
values/styles.xml
<resources>
<style name="AppTheme" parent="#style/Theme.Sherlock.Light">
<item name="android:windowContentOverlay">#drawable/actionbar_shadow</item>
</style>
</resources>
AndroidManifest.xml
<application android:theme="#styles/AppTheme">
actionbar_shadow.9.png
However, I can only see shadow on action bar, but not my custom camera view.
For information, here is how I layout my only Activity.
layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center" >
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="org.yccheok.gui.CustomView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I was wondering, why windowContentOverlay technique is workable for action bar, but not my custom camera view?
You can drop actionbar shadow using this:
<item name="android:windowContentOverlay">#null</item>

Is there a standard way to add dividers between action bar items in Android 3.0?

I have a slight problem trying to customise the look of the action bar in my app. I want to be able to have the pixel wide dividers to group action bar items that you see in many of the native apps (e.g. Gmail, Calendar). I found a way to do this by adding a menu item and setting the 'android:actionLayout' attribute to a custom layout for the divider:
<View
android:background="#color/LightGray"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:layout_width="1dip"
android:layout_height="fill_parent" />
This works nicely, but the issue is it counts as a menu item and the action bar seems to limit the number of menu items to 4 - any others get pushed into the overflow menu.
So I guess what I'm asking is whether there is a standard way to add item dividers without having to use a menu item with a custom view, and in a way that doesn't count towards the limit for action bar items?
Thanks in advance!
I wouldn't try and force dividers into places that the system does not add them automatically as it will make your app inconsistent with the platform. The default behavior is:
Divider between overflow and others.
Divider between text and another item where it would disambiguate which item the text belongs to.
I couldn't find a standard way, but the way I did it was to use the android:actionLayout property for the menu item, and I put the divider in there.
When Google released the 3.0 SDK I got a quick demo app to see how the ActionBar works and just looking back at it, if I use Text Items without Icon drawables, then I get automatic dividers drawn.
My menu.xml file is like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
<item android:id="#+id/menu_text" android:showAsAction="ifRoom" android:title="#string/action_label_text" />
</menu>
Maybe this won't work with icons??
Or thinking about it, maybe the size of the icon has an effect?

Categories

Resources