Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning views in Coordinator Layout

I am trying to create a screen with coordinator layout that includes:

  • Toolbar
  • Fragment, which will be replaced for every page using navigation API
  • Bottom App bar

However, I am struggling with positioning views in a way so the app's bottom bar doesn't overlap what in the fragment.

Any chance you could help me out with this, please? From what I can tell, the only way is to add a margin at the bottom but this may not be consistent across devices.

To illustrate, this list has a total of 25 items but the last two are overlapped by the app's bottom bar.

enter image description here

activity main

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/app_bar_layout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay"
            android:animateLayoutChanges="true"
            android:layout_above="@id/include">

        <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/AppTheme.PopupOverlay"/>

        <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <com.google.android.material.tabs.TabItem
                    android:text="Test 1"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"/>

            <com.google.android.material.tabs.TabItem
                    android:text="Test 2"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"/>
        </com.google.android.material.tabs.TabLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <include
            layout="@layout/content_main"
            android:id="@+id/include"/>

    <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bar"
            android:layout_gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_anchorGravity="right|top"
            app:layout_anchor="@+id/bar"
            android:src="@drawable/ic_add_black_24dp"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content main 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">

    <fragment
            android:layout_width="411dp"
            android:layout_height="627dp"
            android:name="com.example.fitnessfatality.startScreen.StartFragment"
            android:id="@+id/fragment"
            />

</FrameLayout>
like image 284
Jeremi Avatar asked Dec 16 '18 21:12

Jeremi


People also ask

How do you align items in coordinator layout?

You can try gravity to align inside the CoordinatorLayout. android:layout_gravity="end" This worked for me. Save this answer. Show activity on this post.

What is a coordinator layout?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.

Is coordinator layout deprecated?

The CoordinatorLayout. DefaultBehavior annotation is deprecated.


1 Answers

Use these 3 properties to position each other

android:layout_gravity=""
app:layout_anchorGravity=""
app:layout_anchor="@id/xxxx"

Also here is an complete xml that can help you

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
    tools:context=".home.HomeFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        android:id="@+id/app_barlayout"
        android:theme="@style/AppTheme.AppBarOverlay">
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collaps_toolabar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle">
            <LinearLayout
                android:id="@+id/layout_currentMatch_item"
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:orientation="vertical">


            </LinearLayout>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_anchorGravity="bottom"
        app:layout_anchor="@id/app_barlayout"
        android:orientation="horizontal" />

   

</androidx.coordinatorlayout.widget.CoordinatorLayout>
like image 161
Md. Shofiulla Avatar answered Oct 15 '22 07:10

Md. Shofiulla