Hiding Toolbar in tabbed activity with fragment with ScrollView in its ViewPager - java

I want to hide toolBar when scroll View in fragment of ViewPager is scrolled
but when i scroll only scrollView's part is being scrolled (with toolbar in its place).
consider, when I scroll on tablayout (header part, id- tabs) it hides the toolbar
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="5dp"
app:layout_scrollFlags="scroll|enterAlways|snap" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabIndicatorColor="#android:color/white" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
and in My java file is
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("304 Pramukh");
and my fragments Xml file is
<FrameLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Fragments.CommunityFragment">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_family"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</FrameLayout>```

This needed only one minor change,
Just replaced scroll View with Nested scroll view ...

Related

Collapsing toolbar with navigation ui android

I' am trying everything but I don't know how to do a collapsing toolbar with a jetpack navigation fragment. I want this in my screen :
Toolbar (with collapse mode)
Fragment loading by Navigation Jetpack
AdView (from admob)
BottomNavigationView
I tried change order of relative layout and coordinator, but nothing works, in my app.
I have a recycler view on fragments, and when I'm using the scroll, toolbar doesn't hide.
My code is:
<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=".activies.MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adView"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:titleEnabled="false"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="top"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.DayNight"
app:titleTextColor="#android:color/white"
app:layout_scrollFlags="scroll|enterAlways" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<fragment
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<FrameLayout
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_above="#+id/bottomNavigation"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:theme="#style/BottomNavigationTheme"
app:menu="#menu/menu_navigation_bottom">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
Someone can help me? Thank you so much.

Negative margin Layout to overlay parent layout

I have an activity that has a toolbar and a framelayout where I inject fragments.
This is the layout for that activity:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drwDrawerRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_main_selector"
android:theme="#style/AppTheme">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/viewMainToolbar"
layout="#layout/view_toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/frmDrawerContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/toolbar_height"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical" />
<include
android:id="#+id/viewDrawer"
layout="#layout/view_drawer"
bind:name="#{name}"/>
<include
android:id="#+id/viewUserDrawer"
layout="#layout/view_user_drawer"
bind:name="#{name}"/>
</android.support.v4.widget.DrawerLayout>
I have the framelayout with a margin so that the contents from the fragments do not overlay the toolbar, but I set clipChildren and clipToPadding to false as per some other posts I have seen here.
On some fragments however I have a loading view, which I would like to occupy all the screen.
This is a sample fragment:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layout_main"
style="#style/Layout.FullScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layout_footer"
android:orientation="vertical">
<TextView
android:id="#+id/textView7"
style="#style/Text.Header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login_header" />
<EditText
android:id="#+id/edtMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:elevation="1dp"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="#+id/btnSend"
style="#style/Button.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/support_button" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_footer"
style="#style/Layout.FullScreen.Footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="#+id/footerInfo"
style="#style/Text.White.Small.Centered"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/footer_info" />
</LinearLayout>
<include
layout="#layout/view_loading"
android:visibility="invisible"
bind:loading="#{loading}" />
</RelativeLayout>
Ans this is the included loading view, which I have set with negative margin, hoping it would move up to the top of the screen:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-40dp"
android:background="#color/colorLoadingBackground"
android:clickable="true"
android:visibility="#{loading ? View.VISIBLE : View.GONE}">
</FrameLayout>
Am I missing something here?
Is this doable or do I need to change how I am doing this?
Clarification:
The first / root layout includes a toolbar and the second layout is included within the frame layout of the first. this means that the content of the second layout starts below the toolbar. However, I would like the third layout (a loading screen that is included in the second layout) to have a negative margin so that it overlays the full screen, not just starting below the toolbar.
You need to use CoordinatorLayout then you wont need negative margins. here are the sample layout u can customise according to your need
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_behavior= "#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="My App" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<!--include a full screen loading layout here and hide/show according to your need-->
<include layout="#layout/loading_layout/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

Appbar not hiding when recyclerview scrolled

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

HomeAsUp icon showing a bit off

I have this weird issue with the getSupportActionBar().setDisplayHomeAsUpEnabled(true); because it is showing the left arrow a bit off the layout.
This is how the left icon looks
This is the code I use to set the Toolbar as the SupportActionBar
Toolbar toolbar;
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
And this is the activity layout
<?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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dip"
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="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ml.dannyb.packagetracker.workers.SquareImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvPackageDetails"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabEditPackage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dip"
android:layout_marginRight="16dip"
android:src="#drawable/ic_mode_edit_white_24dp"
app:elevation="10dip"
app:layout_behavior="ml.dannyb.packagetracker.workers.ScrollAwareFABBehavior" />
</android.support.design.widget.CoordinatorLayout>
See:
https://www.google.com/design/spec/layout/metrics-keylines.html#metrics-keylines-keylines-spacing
Set:
Status bar height: 24dp
Toolbar height: 56dp
Remove android:fitsSystemWindows="true"
So the problem seems to be Android 5.0+ specific.. I get the indicator placed correctly on other pre 5.0 Android versions and I fixed it by adding a styles-v21/styles.xml file where I extended the base theme.. That was all I needed to do so if anyone else gets this problem now you have the fix! :)

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