CollapsingToolbarLayout in API 21 - android

If you take a look at the CheeseDetailActivity in the cheesesquare app, the CollapsingToolbarLayout seems to cause the Toolbar to be misaligned on API 21:
This also causes the Toolbar to be obscured after scrolling down:
Presumably, it should look like this (API 18 screenshot):
Has anyone encountered this or know of a solution to get the toolbar to align correctly?

Since the v21 theme changes to include:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
The margin needs to be modified on the toolbar for v21 to account for the space that the system bar takes.
dimens.xml
<dimen name="action_bar_margin_top">0dp</dimen>
dimens.xml (v21)
<dimen name="action_bar_margin_top">-24dp</dimen>
Add margin and fitsSystemWindows to the Toolbar.
<android.support.v7.widget.Toolbar
android:layout_marginTop="#dimen/action_bar_margin_top"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />

Related

Android Toolbar Menu Item taking spaces . Don't Set Duplicate

My menu item are taking spaces. I know default padding is 56 dp but I don't know how to remove this. I followed this link but won't work for me.
I am using toolbar not action bar.
my toolbar is
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
I am getting the output like below image ..how to remove spaces between menu item
. ..
Just add this line to your Activity's theme
<item name="android:actionButtonStyle">#style/OptionMenuStyle</item>
and create new style in style resources
<style name="OptionMenuStyle" parent="AppBaseTheme">
<item name="android:minWidth">20dip</item>
<item name="android:padding">0dip</item>
</style>
The problem with setting only 0dp padding is - option menu item has default minimum width, so setting 0dp padding doesn't change anything.
result before applying this style is as below.
result after applying above style
Cheers!!!

How to implement Toolbar Layout

How do I remove the boundary between action and toolbar?
Bad Case
Good Case, I want to like it.
Activity XML
Fragment XML
What you are looking for is a way to remove the shadow under toolbar.
For android 4.4 or below, you will have to set the window content overlay in your activity theme like this
<style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
...
<!--To hide shadow effect in toolbar for Android 4.4 or below-->
<item name="android:windowContentOverlay">#null</item>
</style>
For Android 5.0+, you will also need to set the toolbar elevation to 0 in the activity layout like this.
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
...
>
<android.support.design.widget.AppBarLayout
...
app:elevation="0dp">
...
</android.support.design.widget.AppBarLayout>
</LinearLayout>
Actually, many people ask about this. See answer here
Try to search for existing answer next time ;)

Android Toolbar has more padding than ActionBar, how to fix

I had to replace the actionbar to implement new UI patterns the design team wanted
The actionbar is now replaced with the ToolBar but the toolbar object is bigger than the actionbar, only marginably, but it is noticeable and not desired
Here is the XML declaration
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
How do I make it smaller? I used to be able to rely on actionBarSize which is already predefined for every screensize, I can put in a specific dp value but is that the solution for this object?
Thanks
?attr/actionBarSize is 56dp with new support lib. So even if you stick to actionbar you will get a bigger size. Set actionBarSize in your theme.

Android Toolbar too high in landscape mode

My min and target sdk is 21, and I am not using the support library.
I am using the new Toolbar widget, and generally it works, I just have a glitch in the way it looks. The action icons are centered in portrait mode, whereas the toolbar in landscape mode is higher, and the icons are not centered. Please take a look at the screenshots (read lines are just to make the problem more visible):
portrait:
landscape:
This might look like nothing, but when the actions are selected (like the 'up' arrow at the very left of the bar) the result is that a strip of few pixels below it is visible. I don't like the way it looks.
The activity does not manage any configuration changes itself.
This is my code:
styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Material.NoActionBar">
<item name="android:toolbarStyle">#style/MyToolbarStyle</item>
</style>
<style name="MyToolbarStyle" parent="android:Widget.Toolbar">
<item name="android:background">?android:attr/colorPrimary</item>
<item name="android:elevation">2dp</item>
</style>
</resources>
XML usage:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.TestActivity">
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
I tried messing around with the android:minHeight attribute, but setting to so, say, 30dp just moved the icons even more upwards, also in portrait mode.
I found this: https://code.google.com/p/android/issues/detail?id=77874
But there is no ?attr/actionBarSize...
How, if at all, can this problem be fixed? The phone the screenshots were taken on is a Nexus 6 (xxxhdpi), and the icon is from the google icon pack, and the biggest resource is xxhdpi. Is this the problem?
Update: the workaround from the aforementioned link does work when I use ?android:attr/actionBarSize. But, is this the only way? Seems a bit wrong, to need such workarounds for a new shiny component like this, even more so that the workaround requires an attribute value for a component to be replaced by the new one.
try to use:
android:height="?android:attr/actionBarSize"
for your toolbar.
Add this in your toolbar style:
<item name="maxButtonHeight">?attr/actionBarSize</item>
I had the same problem with android.support.v7.widget.Toolbar.
My solution:
Put your toolbar into a RelativeLayout with the height you want for your toolbar. Set your toolbars layout_height to "wrap_content" and align it to the vertical center. I hope it helps.
<RelativeLayout
android:id="#+id/main_toolbar_relative_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
</android.support.v7.widget.Toolbar>
</RelativeLayout>

Android Lollipop Toolbar Invert Colors

Am using Android L v21 Support library for Android L's Toolbar-ActionBar. I want the items in the toolbar to be white in color. i.e. Navigation Back button, Action Buttons, Overflow Actions. If I use the dark version as the application theme(Theme.AppCompat.NoActionBar) then all the toolbar items are shown in white. But when I use the light version (Theme.AppCompat.Light.NoActionBar) for the application all the toolbar items are shown dark. I want to use the light theme and want to make the toolbar to be show in white.
I have used activity.setTitleTextColor() to change the title's text color but not sure how to change the color for the rest.
SOLUTION:
Thanks to #ianhanniballake
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
Per the AppCompat v21 blog post under Styling:
Styling of Toolbar is done differently to the standard action bar, and is set directly onto the view.
With the example of:
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
Note the app:theme attribute which sets the action bar to use light color text / styling (ThemeOverlay.AppCompat.Dark.ActionBar does the opposite).

Categories

Resources