Muhammad Umar
Muhammad Umar

Reputation: 11782

ConstraintLayout not working over some values

I am using following layout

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <ImageView
            android:layout_margin="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/mainImage"
            android:scaleType="centerInside"
            android:src="@drawable/placeholder"
            />


        <LinearLayout
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            app:layout_constraintLeft_toRightOf="@+id/mainImage"
            app:layout_constraintEnd_toEndOf="parent"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:inputType="text"
                android:hint="Patient Name"
                android:drawablePadding="10dp"
                android:textSize="14sp"
                android:id="@+id/name"
                />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#80000000"
                />

            <EditText
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:inputType="text"
                android:hint="Patient Name"
                android:drawablePadding="10dp"
                android:textSize="14sp"
                android:id="@+id/dob"
                android:visibility="visible"
                />


        </LinearLayout>

        <ListView
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/mainImage"
            android:layout_marginTop="45dp"
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />


    </android.support.constraint.ConstraintLayout>

The result is

enter image description here

The patient name etc should be right of image and list view should be at bottom of image view, why its not recognising constraints

Upvotes: 0

Views: 47

Answers (2)

Prateek Aggarwal
Prateek Aggarwal

Reputation: 166

<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">



    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent">

        <ImageView
            android:id="@+id/mainImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:scaleType="centerInside"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:background="@null"
                android:drawablePadding="10dp"
                android:hint="Patient Name"
                android:inputType="text"
                android:textSize="14sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#80000000" />

            <EditText
                android:id="@+id/dob"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:background="@null"
                android:drawablePadding="10dp"
                android:hint="Patient Name"
                android:inputType="text"
                android:textSize="14sp"
                android:visibility="visible" />
        </LinearLayout>


    </LinearLayout>


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="406dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />


</android.support.constraint.ConstraintLayout>

I justed tested this code on my project and this worked. Just change a few variables according to your project. Thank you! This is what you will get from this code:

enter image description here

Upvotes: 0

Stanislav Bondar
Stanislav Bondar

Reputation: 6245

You need to constraint view from all sides and use match_constraint (0dp) when you need to take all allowed space

<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">

<ImageView
    android:id="@+id/mainImage"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:scaleType="centerInside"
    android:src="@drawable/placeholder"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_marginStart="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="@+id/mainImage"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/mainImage"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toEndOf="@+id/mainImage"
    app:layout_constraintTop_toTopOf="@+id/mainImage">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@null"
        android:drawablePadding="10dp"
        android:hint="Patient Name"
        android:inputType="text"
        android:textSize="14sp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#80000000"
        />

    <EditText
        android:id="@+id/dob"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@null"
        android:drawablePadding="10dp"
        android:hint="Patient Name"
        android:inputType="text"
        android:textSize="14sp"
        android:visibility="visible"
        />


</LinearLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/mainImage"/>


</android.support.constraint.ConstraintLayout>

Upvotes: 1

Related Questions