Reputation: 317
I've six buttons in a vertical chain using a constraint layout. In my emulator it looks fine. You can ignore the two buttons on either side. I added them for perspective.
when I put the app on the the actual corresponding device I get a very different view. The toolbar overlaps my top button and the nav bar overlaps my bottom button.
I've read were the emulator isn't always correct. Given that, how can I account for the height of the toolbar and navigation bar in my constraint layout. Below is my xml. The six buttons are named x21 to x26 in my xml below. I apologize for the length of it. Is there a way to fix this in my xml or do I need to calculate the toolbar and nav bar height in my code and update the margin of the top and bottom button. If I need to do this in my code how would I do this. Or might there be another approach I'm grateful for any advice. Thanks
<com.example.android.cop1803.LayoutCustom 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:id="@+id/mylayoutcustom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context="com.example.android.cop1803.MainActivity"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout_editor_absoluteY="73dp">
<com.example.android.cop1803.MyViewbutton
android:id="@+id/MainCOPbtn"
android:layout_width="@dimen/layoutwidthMaincopbutton"
android:layout_height="@dimen/layoutheightMaincopbutton"
android:layout_marginStart="0dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@drawable/copbuttonmaineffect"
android:text="@string/x11MainCOPtext"
android:textAppearance="@style/MainButtonFontStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x21"
android:layout_width="@dimen/layoutwidthcopbutton"
android:layout_height="@dimen/layoutheightcopbutton"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@drawable/copbutton"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/x21BreadingCrustCoatingsWraps"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toTopOf="@+id/x22"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" />
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x22"
android:layout_width="@dimen/layoutwidthcopbutton"
android:layout_height="@dimen/layoutheightcopbutton"
android:background="@drawable/copbutton"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:text="@string/x22CuttingandManipulation"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toTopOf="@+id/x23"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x21" />
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x23"
android:layout_width="@dimen/layoutwidthcopbutton"
android:layout_height="@dimen/layoutheightcopbutton"
android:background="@drawable/copbutton"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/x23DryRubsandStuffings"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toTopOf="@id/x24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x22" />
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x24"
android:layout_width="@dimen/layoutwidthcopbutton"
android:layout_height="@dimen/layoutheightcopbutton"
android:background="@drawable/copbutton"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/x24HerbandSpicePastes"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toTopOf="@id/x25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x23" />
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x25"
android:layout_width="@dimen/layoutwidthcopbutton"
android:layout_height="@dimen/layoutheightcopbutton"
android:background="@drawable/copbutton"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/x25MarinadesandBrines"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toTopOf="@id/x26"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x24" />
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x26"
android:layout_width="@dimen/layoutwidthcopbutton"
android:layout_height="@dimen/layoutheightcopbutton"
android:background="@drawable/copbutton"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/x26SpicesandSpiceCrusts"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toTopOf="@id/x27"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x25" />
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x27"
android:layout_width="@dimen/layoutwidthcopbutton"
android:layout_height="@dimen/layoutheightcopbutton"
android:background="@drawable/copbutton"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/x27Balance"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x26" />
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x31"
android:layout_width="@dimen/layoutwidthMedcopbutton"
android:layout_height="@dimen/layoutheightMedcopbutton"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@drawable/copbuttonsecond"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:text="@string/x31CookingTechniques"
android:textAppearance="@style/ButtonFontStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.64"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.android.cop1803.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme"
>
<include
android:id="@+id/toolbar"
layout="@layout/toolbar_main"
/>
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/cartlist"
layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
Upvotes: 2
Views: 3214
Reputation: 500
At the top where the ConstraintLayout
is first set up add the last line from below T:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Upvotes: 0
Reputation: 1842
You need to change app:layout_constraintTop_toTopOf="parent"
to
app:layout_constraintTop_toBottomOf="@id/coordinator_layout"
.
When you provide constraint as parent, it takes your parent layout container, which includes the toolbar as well. Instead you want to constraint it below the toolbar.
Upvotes: 0
Reputation: 2545
I think u mean action bar. Try to add action bar margin to the root element of your XML.
android:paddingTop="?android:attr/actionBarSize"
Upvotes: 5