Android Toolbar minHeight redundant? - android

I built my Toolbar like in most with minHeight set to actionBarSize:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:minHeight="?actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
However, if I delete this attribute, there is no difference at all. The Toolbar keeps its actionBarSize, even if I remove the menu and set the app name to an empty String, so there is nothing displayed in the Toolbar at all.
Is there anything I'm missing here?

android:minHeight="?attr/actionBarSize"
minHeight will ensure that your toolbar does not resize itself lower than ?attr/actionBarSize not matter how small the content inside the toolbar is.
EDIT
If there is nothing inside a toolbar and if minHeight is not set, the toolbar will have a default height of 56dp which is equal to ?attr/actionBarSize
So setting minHeight for toolbar is redundant
For more details, What's the height of the Android Toolbar?

This is completely intentional behaviour, The min height of ToolBar is set to action bar height by default . so yea android:minHeight="?actionBarSize" is redundunt . I couldn't find any official document on this but check out this tweet .
https://twitter.com/cyrilmottier/status/814465544279691266

On using both the attributes, android:layout_height="wrap_content" and android:minHeight="?attr/actionBarSize", there is a possibility that your toolbar height may get larger when you use larger icons.
android:minHeight ensures that your toolbar will not resize itself smaller than ?attr/actionBarSize value.
If you want your toolbar height to be fixed, just use
android:layout_height="?attr/actionBarSize"
The other attribute is not needed.

Try this one I did like this it works for me:-
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/AppTheme">
<LinearLayout
.......
/>
</android.support.v7.widget.Toolbar>

Related

How to properly add Custom View in toolbar?

I am adding a custom view in toolbar but it is not using full toolbar width it gives space from left side.
code snippet
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#color/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent">
</LinearLayout>
</android.support.v7.widget.Toolbar>
getting view from this code snippet
Can anyone please tell me Why it is not using full width of toolbar?
Add these 4 attributes in your toolbar it should help you override that effect.
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp" app:contentInsetLeft="0dp" app:contentInsetStart="0dp"
Do let me know if this was of any help.
Actually it is only happens on Android version Lollipop and above. To remove the padding, you can set the content inset to 0dp
Add these to your toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
app:contentInsetStartWithNavigation="0dp"
It has been reported as an issue here https://code.google.com/p/android/issues/detail?id=213826

setting minHeight in CollapsingToolbarLayout not having any effect

The main problem I've currently got with the CollapsingToolbarLayout is, that whatever I'm trying, the minHeight attribute of my Toolbar does not have any effect.
My desired result would be this:
(The CollapsingToolbarLayout with a certain expanded height and a certain collapsed height (in the example 180dp), while the title either collapses or stays on the top)
But whatever I do, the title sometimes is in the center, won't completely collapse or the minHeight is ignored anyway. I have tried to set the minHeight for either AppBarLayout, CollapsingToolbarLayout, the toolbar itself, any contents, etc. also with different approaches found on the web but with no luck.
Here is the basic xml:
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:minHeight="180dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="#android:color/transparent"
app:expandedTitleGravity="bottom"
app:expandedTitleTextAppearance="#style/TextAppearence.App.ToolbarTitle"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:statusBarScrim="#android:color/transparent">
<com.xxxxxx.custom.Banner
android:id="#+id/parallax_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/xxxxxxxx"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.6"
app:layout_scrollFlags="scroll" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="180dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
I hope this is enough content to explain my problem.
Any help is appreciated, thanks!
This one is pretty easy. Just set bottom margin of your Toolbar to whatever space you want to be reserved in the collapsed version.
The reasoning behind this is simple. CollapsingToolbarLayout inherits from FrameLayout which stacks images on top of each other. The only things it takes in account when considering collapsed bounds are toolbar height and its top / bottom margins.
You can set the height to 180dp instead of using minHeight, and then add a TextView into the toolbar to serve as the title.
just set Toolbar collapseMode as "pin" mode, and height with your requirement size
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>

CollapsableToolbarLayout with custom Toolbar height

I'm creating a layout using a CoordinatorLayout holding an AppBarLayout with a CollapsableToolbarLayout and a Toolbar. Below the app bar is a RecyclerView.
When scrolling in the RecyclerView, the Toolbar should collapse, similar to the CheeseSquare demo app. The difference from that demo is that 1) I want my Toolbar (including the title) always statically at the top (so I've set app:titleEnabled on my CTL), and 2) I don't want to collapse the app bar fully into only showing the single line Toolbar since I have some text views etc below the Toolbar which the app bar should never collapse beyond. I am using a custom Behavior to control these text views during collapsing, and all this works fine.
Problem is, I don't seem to grasp how to best set the anchor point at which the collapsing will end. I know that the app bar will collapse until it snaps to to the height of the Toolbar. So I would need to either put my custom views inside the toolbar, hence increasing the height of the Toolbar itself, or somehow instruct the CTL to anchor to my custom view instead. However, whenever I change the height of the Toolbar, the title text gets strangely aligned and I haven't found a good way of keeping it properly aligned with any set of reasonable attributes.
Question is, should I be playing with the height of the Toolbar at all, or is there a way of getting the CTL to stop collapsing at some other anchor point than the bottom of the Toolbar?
Or should I indeed modify the height of the Toolbar and place my own layout in it that includes the title text, and manually make sure to align it vertically with the toolbar icons etc? The best I've managed so far is to place a LinearLayout containing a TextView inside the Toolbar, with a hard-coded layout_marginTop="14dp", which might have compat issues. It seems really strange that it wouldn't be possible to increase the height of a Toolbar while still maintaining the default title text position.
Or should I not use the CTL at all but instead create my completely custom behavior also for the parts that collapses the app bar during initial RecyclerView scroll events?
I've searched for similar questions obviously, and the closest I've found is this question which does not address my problem with the title text getting messed up.
My current layout, including the ugly hard-coded manual title TextView inside the Toolbar:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="180dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="110dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="14dp">
<TextView
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Title"/ >
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:elevation="10dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_anchor="#id/toolbar"
app:layout_behavior="my behavior class">
<!-- My custom views to be shown in the app bar, animated through custom behavior -->
</LinearLayout>
<android.support.v7.widget.RecyclerView android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>

Change android toolbar background color alone

<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res`enter code here`/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar">
When i try to set toolbar alpha value as 0 at runtime it also hides the toolbar icons.
i need to set the toolbar background color as red at runtime with dynamic alpha.
please help me to fix this.

Extended Toolbar with Custom View not Displaying with Full Width

I went through lots of answers here related to Toolbar but none of the answers could help me.
What I'm trying to achieve is to have an extended toolbar whhich will display a logo, possibly a name of the Activity/App, it will have an action button/drawer toggle to the right that will display a navigation-like drawer to the right, an overflow menu with other options like settings and also a navigation Tab at the bottom that will allow the user to move between different fragments (different days in a month).
The way I'm trying to achieve this is through toolbar.
First I create the toolbar and add my custom view.
<?xml version="1.0" encoding="utf-8"?>
<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_actionbar"
android:layout_width="match_parent"
android:layout_height="#dimen/min_double_toolbar_height"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:minHeight="?attr/actionBarSize"
android:background="#color/primary"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
android:clipToPadding="false"
>
<!-- ThemeOVerlay Makes sure the text and items are using solid colors-->
<include
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="bottom"
layout="#layout/day_navigation_tab"
/>
</android.support.v7.widget.Toolbar>
day_navigation_tab is just a horizontal with Two image views with Padding of 72dp (as by guidelines) and a text view with layout weight set to 1 so it stretches for the whole available space. And a height of 72dp.
Now in my BaseActivity XML I include the toolbar in the Framelayout of the main context (otherwise the toolbar would cover the whole screen for an unknown reason for me (took me ages to figre that out))
<...ommited>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/app_bar"
/>
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="right"
android:name="alterway.agency.timeentry.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer"
app:layout="#layout/fragment_navigation_drawer"
/>
</android.support.v4.widget.DrawerLayout>
However, when I use the toolbar I inflate the Menu on the actionBar in onCreateOptionsMenu my custom view gets shrinked and won't extend the edge of the created options.
A picture below illustrates that better, It's a screenshot from a Design View in Android Studio.
Yellow rectangle indicates where the option items will be placed.
Purple indicates the original size in the DesignView. Black indicates the size when running the app.
Thank you for any help.
Related questions that might be useful to anybody who comes across this looking to solve a similar problem:
design - Support Toolbars custom view not using full width
How do I set up an extended toolbar on Android L
So in order to achieve this and have complete control over the padding in the toolbar I created two toolbars. The first toolbar with standard height of 56dp and second with height of 72dp which together made a double layered toolbar as specified by material design.
And because I am not inflating any menu items in the second toolbar all my cusotm views inside behave as desired.
These lines still need to be used though
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
android:clipToPadding="false"
This solved my issue so now I'm including two toolbars into my XMl.
since versions 23 of the design support libraries there is a much simpler way to do it using android.support.design.widget.CoordinatorLayout
This is an example:
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/project_light_green"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin_half"
android:paddingRight="#dimen/activity_horizontal_margin_half"
android:paddingTop="#dimen/activity_vertical_margin_half">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="12dp"
android:src="#drawable/ic_search_24dp" />
<EditText
android:id="#+id/search_field"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#null"
android:hint="#string/autocomplete_hint"
android:singleLine="true"
android:textColor="#color/project_black"
android:textColorHint="#color/project_dark_gray"/>
<include layout="#layout/close_button"
android:id="#+id/clearButton"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_search_location" />
</android.support.design.widget.CoordinatorLayout>
From the Toolbar documentation:
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.
So from what it look like, your custom view is behaving as it should. The options menu is taking up a certain amount of space in your toolbar, so the Toolbar is measuring the remaining space for your custom view to fit in.

Categories

Resources