Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ConstraintLayout Margins not working correctly

As seen in the picture, the view boxes are rendered at the current position, but the content is not. The text views as well as the image views are rendered as if there weren't any margins. When I run the app, they also aren't rendering properly(second image). I don't know what could be causing it.Editor Preview Live Demo

EDIT: I forgot to include my xml:

<?xml version="1.0" encoding="utf-8"?>
<com.example.android.stundenplanexample.dynamic_recyclerview.ExpandingCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp"
    android:layout_margin="12dp">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_margin="8dp"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_weight="0"
            android:src="@drawable/ic_expand_more_black_24dp"
            android:id="@+id/card_expand_toggle"
            tools:ignore="ContentDescription"
            android:layout_marginTop="16dp"
            card_view:layout_constraintTop_toTopOf="parent"
            card_view:layout_constraintRight_toRightOf="parent"
            android:layout_marginEnd="24dp" />

        <TextView
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:textAppearance="@android:style/TextAppearance.Material.Medium"
            android:textColor="?android:attr/textColorPrimary"
            android:layout_width="42dp"
            android:layout_height="wrap_content"
            android:text="@string/card_expand_string"
            android:id="@+id/textView3"
            card_view:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="16dp"
            card_view:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginTop="16dp" />

        <TextView
            android:layout_height="wrap_content"
            android:id="@+id/extra_information"
            android:layout_width="wrap_content"
            tools:text="ToleToleToleInfo"
            card_view:layout_constraintLeft_toLeftOf="@+id/textView3"
            android:layout_marginTop="8dp"
            card_view:layout_constraintTop_toBottomOf="@+id/textView3"
            card_view:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="16dp" />
    </android.support.constraint.ConstraintLayout>

</com.example.android.stundenplanexample.dynamic_recyclerview.ExpandingCardView>
like image 292
BBotMerlin Avatar asked Aug 10 '17 22:08

BBotMerlin


2 Answers

I see one definite problem and one potential problem.

The definite problem is that your textView3 specifies these attributes:

android:layout_margin="8dp"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"

This does not set the top/start to 16dp and the end/bottom to 8dp. It simply causes the marginTop and marginStart attributes to be ignored.

The potential problem is that you specify

android:layout_marginStart="16dp"

but do not also specify marginLeft. If you want to support pre-API 17 devices, you need to specify both marginStart and marginLeft (assuming you remove the margin attribute).

like image 182
Ben P. Avatar answered Oct 03 '22 01:10

Ben P.


Make sure you set start and end constrains before setting start and end margins. I had same problem and it worked after setting end constrains .

like image 30
Hitesh Sahu Avatar answered Oct 03 '22 00:10

Hitesh Sahu