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>
Related
In my app, when the keyboard is invoked, the toolbar is moved upwards. How do I prevent the toolbar from moving upwards?
Here's the code to make the activity full screen:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Layout:
<RelativeLayout xmlns:android=".."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/toolbar_background">
<include layout="#layout/layout_toolbar" />
</android.support.design.widget.AppBarLayout>
<ScrollView.......>
<RelativeLayout......>
<EditText....><!--There are multiple EditTexts here.-->
</RelativeLayout>
Add this line in menifests for Activity
android:windowSoftInputMode="adjustResize"
add android:fitsSystemWindows="true" in your toolbar's root layout
Most of the time, your app won’t need to draw under the status bar or
the navigation bar, but if you do: you need to make sure interactive
elements (like buttons) aren’t hidden underneath them. That’s what the
default behavior of the android:fitsSystemWindows="true" attribute
gives you: it sets the padding of the View to ensure the contents
don’t overlay the system windows.
A few things to keep in mind:
fitsSystemWindows is applied depth first — ordering matters: it’s the
first View that consumes the insets that makes a difference
from https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec
basically you're telling the toolbar not to move & stay on top & instead move the views underneath it upwards below it
You can use a CollapsingToolBarLayout if it fits your UI requirement inside of the AppBarLayout.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
android:id="#+id/tv_toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
ripple:contentInsetStart="0dp">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
I encountered a strange problem with the new Android Design Support Librar (http://android-developers.blogspot.com.ar/2015/05/android-design-support-library.html). If I place additional content (like a LinearLayout) in an AppBarLayout along with the ToolBar and toggle the visibility of that content then switching fragments will have show a dead space at the top of the fragment content.
It appears that AppBarLayout isn't resizing the parent CoordinatorLayout correctly when visibility of the content is toggled. I have my CoordinatorLayout wrapped in the DrawerLayout. I want to toggle the visibility of the extra LinearLayout in the AppBarLayout depending on the which fragment shown.
Here is my main.xml file for the MainActivity:
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/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="?attr/actionBarSize"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:text="Hello"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
I had a similar problem using the support design widget. I had a CoordinatorLayout inside a DrawerLayout and an AppBarLayout inside the CoordinatorLayout. I had two toolbars inside the AppBarLayout. My aim was to display a toolbar with a ViewPager displaying a recyclerview content. I wanted to swop between the toolbars when selecting items. In other words, I made one toolbar GONE while the other was visible and vise versa. Scrolling the content up would push the toolbar up off the top of the screen. Everything worked perfectly except that changing orientation would show a space for the toolbar that should be gone. I tried every hack I could think of to get rid of it but did not succeed. I then came across this post and realized that it was a bug in the support library. I then tried putting a FrameLayout in the AppBarLayout and then put the two toolbars inside the FrameLayout and NO MORE SPACE! everything now works as I intend it to work. GONE toolbars are GONE and only the visible toolbar shows, even when changing the orientation.
Hope this helps someone.
Related Questions
CollapsingToolbarLayout | Scrolling and layout issues 2
Question
I have been working with the Android Support Design Library and successfully implemented the CoordinatorLayout that causes the Toolbar and TabLayout to scroll out of view when scrolling. This works very well, so I figured I would try my luck with the new CollapsingToolbarLayout.
In a seperate activity, I have been having issue-after-issue with implementing CollapsingToolbarLayout. I am, as they say, close but no cigar.
I want to use 2 different fragments
Header Image (Currently just an ImageView)
"Scrollable" content (actually content isn't really scrollable, but I forced it will long Lorem Ipsum text for testing)
I had built one example of this layout by myself, but could not get it working after many tries. Finally, I found this [enter image description here][5] and modified it to get to the point I am now
Issues
Note: I don't know if these are caused by 1 thing (dominoes effect), or if these are individual. Also, I have looked at quite a few related questions, but none seem to have any of these issues.
Scrollable Content is shown above the Header Image
Scrollable Content is not anchored to the bottom of the Header Image
On Scrolling of Scrollable Content it will scroll the Header Image seemingly randomly of either:
Just right and follows the speed of the finger (perfect)
Too fast and will animate the Header Image off the screen by moving my finger the height of 1 line of text
Also on Scroll down, the 2 above effects happen along with a 3rd effect
Instant or Near instant "animation" of showing the Header Image at full width
Edit: These below are asked in another question!! The above had 1 simple fix
The CollapsingToolbarLayout does not allow me to expand the Toolbar to see the full Header Image
It shows a majority of the image, but not all. Top is cut, but the bottom is visible.
The Toolbar is set to Pin but it is hidden when scrolling
Just the Header Image should disappear
Code
General Layout
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout>
<ImageView/> <!-- Will be a fragment later -->
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView>
<fragment/> <!-- Not a scrolling fragment layout -->
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Layout.xml
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="16dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|enterAlways">
<ImageView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/download"
android:scaleType="centerCrop" />
<android.support.v7.widget.Toolbar
android:id="#+id/anim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imageView1">
<fragment
android:id="#+id/detail"
android:name="<package>.<fragment_name>"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
1
2
3
4
5
6
You need to add app:layout_behavior="#string/appbar_scrolling_view_behavior" to your NestedScrollView - this marks the class which should be positioned below the AppBarLayout (and hence, below the CollapsingToolbarLayout).
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.
The new Android Toolbar gives us some flexibility in what we can do with the actionbar, but I have ran into an issue. I'm using a ToolBar in the layout of my activity as an actionbar('AppBar' is correct name now I guess). When user focuses on a edittext the content is scrolled up by the system to let the user see the field. It also scrolls the ToolBar up and off the screen. This is a problem because navigation is on the Toolbar and now hidden from the user.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize">
</Toolbar>
</LinearLayout>
How can we keep the ToolBar in place?
You can do this by placing a ScrollView below the Toolbar and putting content in it. Also will need to set android:windowSoftInputMode="adjustResize" on the activity. Also, to avoid the actionmode pushing the toolbar down, add this attribute
android:windowActionModeOverlay="true"