It seems that icons and text menu items on a Toolbar take different paddings
Google doesn't seem to implement their design specification. I think icons take 12dp but text menu items take just 8dp. What's the right way to give text menu items additional padding so that they match icons' paddings?
What is the canonical approach to handling this inconsistency?
Thanks
Related
As of Android L, we have a Toolbar instead of the ActionBar although its usages seems the same. They even made back compatibility for Toolbar via support library.
What was the reason they replaced ActionBar with a Toolbar?
Toolbar was added because UIs have evolved past the limitations of the ActionBar. The main difference is that the Toolbar can be decoupled from an Activity's opaque window decor and placed in your custom layout somewhere. From there, you have the freedom to do some more interesting things with the Toolbar. One common example is growing or shrinking the height based on scrolling.
From the Toolbar Documentation.
A Toolbar is a generalization of action bars for use within
application layouts. While an action bar is traditionally part of an
Activity's opaque window decor controlled by the framework, a Toolbar
may be placed at any arbitrary level of nesting within a view
hierarchy. An application may choose to designate a Toolbar as the
action bar for an Activity using the setActionBar() method.
Toolbar supports a more focused feature set than ActionBar. From start
to end, a toolbar may contain a combination of the following optional
elements:
A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing.
This button should always be used to access other navigational
destinations within the container of the Toolbar and its signified
content or otherwise leave the current context signified by the
Toolbar. The navigation button is vertically aligned within the
Toolbar's minimum height, if set.
A branded logo image. This may extend to the height of the bar and can be arbitrarily wide.
A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content
contained there. The subtitle, if present should indicate any extended
information about the current content. If an app uses a logo image it
should strongly consider omitting a title and subtitle.
One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the
layout. If a child view's Toolbar.LayoutParams indicates a Gravity
value of CENTER_HORIZONTAL the view will attempt to center within the
available space remaining in the Toolbar after all other elements have
been measured.
An action menu. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along
with an optional overflow menu for additional actions. Action buttons
are vertically aligned within the Toolbar's minimum height, if set.
In modern Android UIs developers should lean more on a visually
distinct color scheme for toolbars than on their application icon. The
use of application icon plus title as a standard layout is discouraged
on API 21 devices and newer.
I need to develop a custom layout for the action bar in Android.
I'm now stuck because I have no clue how to calculate the available space for my custom layout.
In the image, the red part is my custom layout, the blue part is the space occupied by the action buttons.
Clearly I need to know/calculate the size of the red part to be able to correctly position the elements in my custom action bar (for example center the title in the window or make sure not to overflow in the blue part).
How can I achieve this?
I couldn't find useful examples or a clear API in the Android documentation.
I think that anyone using the custom action bar layout must be facing this kind of problem, I'm a bit confused.
Or am I supposed not to use action buttons in this case? Perhaps I'm supposed to replicate the action buttons by myself?
Thanks in advance
Try using ToolBar instead of ActionBar. It will give you a lot more control over ActionBar elements than ActionBar.
Here is a simple tutorial about how to replace ActionBar with ToolBar and how you can customise it.
Material ToolBar
Possibilities are endless with ToolBar but my recommendation is to don't overdo anything.
As per my suggestion try to use the entire action bar by your custom layout. So that you can arrange all the things use it as a fragment inside a framelayout for all your screens so that the code can be reused for all your layouts.
Basically, I want to make sure my UI elements (TextViews, Buttons, etc) are left aligned with the ActionBar logo. My idea was to try to find out the left spacing between the edge of the screen and the left side of the logo (shown highlighted in blue in the attached image), and use this value as the left margin for the UI elements in my Activity, but I've been unable to get this value.
So, how can I get the value of this margin? Or, is there a better way of accomplishing this?
Thanks.
Just use the standards for defining paddings as described in the Android Design documents on d.android.com. This page gives a good example of how your layout must be structured in terms of paddings between elements.
You check these answers . Custom layout for the action bar worked for me to change this padding and you can also get padding value.
remove padding around action bar left icon on Android 4.0+
I have a similar problem as found on this specific question.
I'm using a Toolbar from the v7 support library, and a custom layout. If I have no options menu, I have a result similar to what OP has on the question linked above:
However if I have an options menu, it shifts the toolbar a little bit to the left (start). I would like to have it over the toolbar.
Is it possible? The only way I could hack it is by adding a negative right (end) margin to the toolbar layout, however this moves the menu outside of the screen…
Initially, the whole Toolbar contained several views, so they had to share the width with the action menu.
Now instead I wrapped the Toolbar and the other views in a container (a FrameLayout), so that they overlap without sharing the width. I just need to be careful with the margin at the top so that the other views don't overlap with action icons.
Statements such as android:layout_centerInParent="true", does it center by taking the actionbar into account? Because when I use two backgrounds both of the same height that meet in the middle using a fake view that is centered in the parent, the top baclground loooks squished and I think it might be because of the action bar. Hopefully this picture can show what I mean...
Usually the action bar is placed above the rest of your layout. If you want it to be drawn in front of your layout you have to enable overlay mode. Having that in mind, most probably the vertical center of your layout is the center of the area below the action bar (e.g. the leftmost item in your diagram)
You can find more info about the overlay mode at the end of the Removing the action bar section of the ActionBar dev guide.