How to add bottom view to a Coordinator Layout with view pager?
Asked Answered
P

3

35

I want to add a bottom view to a Coordinator layout with view pager in it , Bottom View will be on top of fragment loaded by view pager and independent of it .

I added a linear layout with

layout_gravity = "bottom"

but bottom view linear layout is not showing at all

Following is my xml layout of the activity.

<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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/maintoolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    <android.support.design.widget.TabLayout
        android:id="@+id/maintabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill" />
</android.support.design.widget.AppBarLayout>





<android.support.v4.view.ViewPager
    android:id="@+id/mainviewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />


<LinearLayout
    android:id="@+id/completeBottomView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressBarBottomView"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:indeterminate="false"
        android:visibility="gone"
        android:max="100"
        android:progress="1"/>

    <HorizontalScrollView
        android:id="@+id/limiter_scroller"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:background="#FF3399"
        >
        <LinearLayout
            android:id="@+id/limiter_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:onClick="clickFromBottomView"/>
    </HorizontalScrollView>

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>
Perturbation answered 16/3, 2016 at 21:11 Comment(1)
add completeBottomView inside framelayout tryHydrostat
P
54

As pointed out in comment by @Dhawal ....Solution is to wrap LinearLayout completeBottomView in a FrameLayout with android:layout_gravity="bottom"

Perturbation answered 20/3, 2016 at 16:46 Comment(5)
this fixed my problem. had an edittext anchored to the bottom that i needed appear about the keyboard when it gained focus. cheersGaribaldi
Adding android:layout_gravity="bottom" directly to your LinearLayout (completeBottomView) should work as well. I don't think the additional FrameLayout is necessaryVadnais
Doesn't this mean that the content of the bottom view will overlap the other content?Byler
Any solution? The bottom view is covering part of the content of my viewPagerWateriness
Not a solution specially if you need translucent bar with fitsSystemWindowsMientao
G
7

Android CoordinatorLayout Bottom Layout Behaviour.

activity_bottom.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.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        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"
            android:background="?attr/colorPrimaryDark"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C0C0C0"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="bottom"            
        app:layout_behavior="com.utils.behaviour.BottomLayoutBehavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#007432"
            android:gravity="center"
            android:text="Footer View"
            android:textColor="@android:color/white"
            android:textSize="25sp" />
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

BottomLayoutBehavior.kt

package com.utils.behaviour

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import kotlin.math.max
import kotlin.math.min

class BottomLayoutBehavior<V : View>(context: Context?, attrs: AttributeSet?) :
    CoordinatorLayout.Behavior<V>(context, attrs) {

    override fun onStartNestedScroll(
        coordinatorLayout: CoordinatorLayout, child: V,
        directTargetChild: View, target: View, axes: Int, type: Int
    ): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }

    override fun onNestedPreScroll(
        coordinatorLayout: CoordinatorLayout, child: V,
        target: View, dx: Int, dy: Int, consumed: IntArray, type: Int
    ) {
        child.translationY = max(
            0f,
            min(child.height.toFloat(), child.translationY + dy)
        )
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
    }
}
Goof answered 19/7, 2018 at 14:11 Comment(0)
O
0

This code worked for me. Change app:layout_scrollFlags="scroll|enterAlways" to app:layout_scrollFlags="scroll" attribute of android.support.v7.widget.Toolbar. More details here

Osmo answered 25/5, 2017 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.