Reputation: 528
I'm trying to add CoordinatorLayout
including a Toolbar
inside of a Fragment layout. But the problem is that the contents of the NestedScrollView
Overlapping to the Toolbar
, as you see in image below :
I tried to add a
RelativeLayout
parent and useandroid:layout_below="@+id/coordinatorlayout"
for theNestedScrollView
,but this make the content ofNestedScrollView
not showing up.
Fragment layout Code :
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorlayout"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:background="@color/colorPrimary"
android:id="@+id/toolbar2"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#FFFFFF">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<android.support.v7.widget.CardView
android:layout_height="40dp"
android:layout_width="match_parent">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="@+id/search_icon"
android:layout_alignParentRight="true"
android:layout_height="24dp"
android:layout_width="24dp"
android:src="@drawable/ic_search_black_24dp" />
<EditText
android:hint="Search"
android:id="@+id/searchEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CoordinatorLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="hello"/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
Upvotes: 1
Views: 2667
Reputation: 21736
1. Use CoordinatorLayout
as root
layout.
2. Add AppBarLayout
and NestedScrollView
inside CoordinatorLayout
.
3. Put Toolbar
inside AppBarLayout
.
Here is the working 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"
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.support.v7.widget.Toolbar
android:background="@color/colorPrimary"
android:id="@+id/toolbar2"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#FFFFFF">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<android.support.v7.widget.CardView
android:layout_height="40dp"
android:layout_width="match_parent">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="@+id/search_icon"
android:layout_alignParentRight="true"
android:layout_height="24dp"
android:layout_width="24dp"
android:src="@drawable/ic_search_black_24dp" />
<EditText
android:hint="Search"
android:id="@+id/searchEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="hello"/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
OUTPUT:
Hope this will help~
Upvotes: 1
Reputation: 528
Thanks to @tompadre
The one and only solution was that i have to change the width="match_parent"
of both CoordinatorLayout
and NestedScrollView
to wrap_content
and the add this code below to NestedScrollView
:
android:layout_below="@+id/coordinatorlayout"
a really simple problem wasted my time for an hour :/
Upvotes: 1