Appbar not hiding when recyclerview scrolled - java

I am trying to hide appbar when user scrolled recyclerview layout code as per follows:
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/my_appbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/plp_header_card_view"
app:layout_scrollFlags="scroll|enterAlways" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/item_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:scrollbars="none" />
</android.support.design.widget.CoordinatorLayout>
I want to hide appbar when this screen intialize & show appbar when user scroll the recyclerview. Please help me to do this one.

The below code is proper way to make toolbar hide when the scroll down.
<?xml version="1.0" encoding="utf-8"?>
<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.support.design.widget.AppBarLayout
android:id="#+id/tabanim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/tabanim_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

Apply app:layout_scrollFlags="scroll|enterAlways" on LinearLayout work hide action recycleview scroll.
But you question is hide appbar when this screen intialize & show appbar when user scroll the recyclerview
My suggestion as belowit will help you to achieved target
1. Hide and show toolbar on scroll
2. you can use CollapsingToolbar

Related

How to make my fragment fit between my toolbar and bottom navigation bar and still make it scrollable?

So I will have my main-activity layout file below so you can understand what is happening
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<include
android:id="#+id/toolbar"
layout="#menu/toolbar"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_nav">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
app:itemIconTint="#android:color/white"
android:background="#39b54a"
app:labelVisibilityMode="unlabeled">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
The problem with this layout is that the toolbar on the top and the bottom navigation bar cover content from my fragments. Also one of my fragments has got a recyclerview with many items and I canĀ“t scroll down for some reason ? Is there a way to make the fragments fit exactly between the toolbar and bottomnavigation like resizing itself to fit into that area without cutting content and still make it scrollable ?
Do it like this ...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_layout"/> <!--include your layout file here -->
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="#id/toolbar"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_nav">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav_menu"
app:itemIconTint="#android:color/white"
android:background="#39b54a"
app:labelVisibilityMode="unlabeled">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
And by the way, you are supposed to set a layout resource file into the toolbar layout while including but you are passing the menu resource file. Change that also accordingly...

How to make an object constatnt at collapsing toolbar layout?

I am making an app that develop a profile activity
my profile activity has a collapse toolbar layout, how to make some view constant under the toolbar while scrolling the screen, these are screen shots of what I mean
before collapsing
This what I want to make
So , just create layout named activity_profile_detail and past this code
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
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="?colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/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>
//your main content here
</android.support.design.widget.CoordinatorLayout>

Toolbar image animation collapse style

I have a Toolbar that when i get to the Activity has an image expanded and the Toolbar title at the bottom of the image, and when i scroll the content below the Toolbar, the image disappears and the toolbar title moves to the top position.
The easiest way i can explain it is in the attached image:
The code i currently have is below:
<android.support.design.widget.CoordinatorLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout_list_item"
android:fitsSystemWindows="true"
tools:context=".ui.activities.ItemBuyNowActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:background="#color/orange_6"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_buy_now_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<android.support.v7.widget.Toolbar
android:id="#+id/buy_now_tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/list_item_cart_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/cart_empty_dark"/>
</android.support.design.widget.CoordinatorLayout>
EDIT: The long lost question is, that i would like my toolbar to appear below the image, and when scrolled up it will stop at the top and the image would have disappeared.
Currently, the Title is at the bottom of the image but overlaying the image, and when scrolled to the top the title is at the top and the image had disappeared as its meant to.
The problem is that i need the toolbar to be below the image when the toolbar is fully expanded!
Thanks
Okay, this already so easy.
Replace your current code with this:
<?xml version="1.0" encoding="utf-8"?>
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="16dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- This is the image you want to add -->
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<!-- Toolbar to just hold the action icons -->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!-- Nested scroll to make the content under the bar scrolls and move up and down the collapsing toolbar -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<!-- YOUR CONTENT -->
<include layout="#layout/content_detail" />
</android.support.v4.widget.NestedScrollView>

Hiding Toolbar when setting Recycleviewer background

When I set the background paramter on my RecyclerView, it hides the Toolbar.
Simple, but I can't figure out how can I fix it.
activity_main.xml:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#android:color/darker_gray"
tools:context="los.printers.MainActivity"
app:theme="#style/Theme.MyTheme">
<android.support.v7.widget.Toolbar
android:id="#+id/tb_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<RelativeLayout
android:id="#+id/rl_fragment_container"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
fragment_printer.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="los.printers.fragment_printer">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_List"
android:scrollbars="vertical"
android:paddingTop="?attr/actionBarSize"
android:background="#color/colorTextIcons"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
The padding works fine in RecyclerView, but the toolbar doesn't shows up.
Your rl_fragment_container is drawn on top of your toolbar.
But I think you know that because you apply a padding in your fragment.
A padding will draw the view in the same size and is applied to its contents, while a margin will be outside of the views bounds, making the whole view smaller. This is important if you draw backgrounds.
As said befoer, your fragment is on top of your toolbar, your recyclerview fills the whole fragment and draws a background on top.
You can now
move the rl_fragment_container before the toolbar in xml -> this will get it drawn below the toolbar
change padding to margin
or just layout the rl_fragment_container below your toolbar with android:layout_below="#+id/tb_main" and remove paddings & margins.
Update the activity_main.xml:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#android:color/darker_gray"
tools:context="los.printers.MainActivity"
app:theme="#style/Theme.MyTheme">
<android.support.v7.widget.Toolbar
android:id="#+id/tb_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<RelativeLayout
android:id="#+id/rl_fragment_container"
android:layout_below="#+id/tb_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Framelayout doesn't show up in main activity

This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include android:id="#+id/toolbar"
layout="#layout/toolbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/toolbar">
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</LinearLayout>
My toolbar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
The fragment I send to fragment_content:
<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"
android:orientation="vertical"
android:background="#ffffff">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_below="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Listview -->
<ListView
android:id="#+id/lvEvent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:divider="#D0D0D0"
android:dividerHeight="0.5dp"
android:showDividers="middle"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Now the toolbar show up, I can do actions with it but there is nothing in my Framelayout? If I delete the include (toolbar) then I can see the content in my Framelayout. Anybody know what I'm doing wrong?
Given that you have set the height of your toolbar to wrap_content, I assume you want a vertical layout, but your top level LinearLayout is set to horizontal. Change the orientation to vertical.

Categories

Resources