I am creating a layout
.I want to set my custom toolbar
(Which is another layout) to the top of layout
and the FrameLayout
just below the toolbar
. But the FrameLayout
should get rest of the layout
's height (match_parent). How to achieve this using constraint layout ?
Asked
Active
Viewed 1.1k times
4

Vadim Kotov
- 8,084
- 8
- 48
- 62

Yogesh Katkar
- 369
- 1
- 4
- 16
-
Possible duplicate of [Set width to match constraints in ConstraintLayout](https://stackoverflow.com/questions/37603751/set-width-to-match-constraints-in-constraintlayout) – Mr. Roshan Nov 14 '18 at 11:23
-
Please see my solution and check if it is exactly how you need. – Ümañg ßürmån Nov 15 '18 at 05:11
4 Answers
12
Solution: I'm assuming that you have your toolbar as another xml file, something like this:
toolbar.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dp"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
then using ConstraintLayout
use it with FrameLayout
like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
</android.support.constraint.ConstraintLayout>
Hope this helps. Please comment if you have any issues with it.

Ümañg ßürmån
- 9,695
- 4
- 24
- 41
11
Use 0dp
that is match_contraint in ConstraintLayout.
Like android:layout_height="0dp"

Pankaj Kumar
- 81,967
- 29
- 167
- 186
-
Thanks for the answer @Pankaj. I already did it, but did not help. – Yogesh Katkar Nov 14 '18 at 11:22
-
4Not only `android:layout_height="0dp"` but adding `app:layout_constraintBottom_toBottomOf="parent"` this line solved my problem. – Yogesh Katkar Nov 14 '18 at 11:29
-
@YogeshKatkar Yes that will required as you are using ConstraintLayout you need to set constraints. I thought you would be doing all these things, only height is not working. – Pankaj Kumar Nov 14 '18 at 11:30
-
1
give the toolbar an id ,
and in your frame layout set the width and height to 0dp
add the constraints like in this code..
<android.support.constraint.ConstraintLayout
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=".Main2Activity">
<Toolbar
android:id="@+id/toolbar"
app:layout_constraintTop_toTopOf="parent"
android:background="#ab1010"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Toolbar>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#f1f1df"
>
</FrameLayout>
</android.support.constraint.ConstraintLayout>

Outlandish
- 243
- 1
- 2
- 13
0
Note, that unlike other viewgroups in android, constraintlayout should have circular dependencies between the child views. Herewith, you should use 0dp instead of 'match_parent' width and constraints for each side of the child view.
<android.support.constraint.ConstraintLayout
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.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>
</android.support.constraint.ConstraintLayout>

Onix
- 662
- 3
- 10