Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constraint layout left and right constraint

I have a constraint layout and when the string is long enough it overlaps the element on the right. Sample code is:

  <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  <ImageView
    android:id="@+id/iconIv"
    android:layout_width="36dp"
    android:layout_height="36dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>



  <TextView
        android:id="@+id/nameTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/iconIv"
        app:layout_constraintTop_toTopOf="parent"/>

  <FrameLayout
    android:id="@+id/priceFl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">
  </FrameLayout>

 ...

enter image description here

Then I tried to nameTv to add right constraint to left of the element on the right like this app:layout_constraintRight_toLeftOf="@id/priceFl" and this happened:

enter image description here

Is there a way to position this text between icon and price views?

like image 650
Rustam Ibragimov Avatar asked Jan 22 '18 08:01

Rustam Ibragimov


2 Answers

Try this

<TextView
        android:id="@+id/nameTv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/iconIv"
        app:layout_constraintRight_toLeftOf="@+id/priceFl"
        app:layout_constraintTop_toTopOf="parent"/>
like image 136
Mayank Panchal Avatar answered Oct 11 '22 10:10

Mayank Panchal


Try this, the width is 0dp and using app:layout_constraintHorizontal_weight you can adjust the width as per your requirement.

<ImageView
   android:id="@+id/iconIv"
   android:layout_width="36dp"
   android:layout_height="36dp"
   android:src="@mipmap/ic_launcher_round"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintEnd_toStartOf="@+id/nameTv"
   app:layout_constraintTop_toTopOf="parent" />

 <TextView
    android:id="@+id/nameTv"
    android:layout_width="0dp"
    app:layout_constraintHorizontal_weight="2"
    android:layout_height="wrap_content"
    android:text="Some text"
    app:layout_constraintStart_toEndOf="@+id/iconIv"
    app:layout_constraintEnd_toStartOf="@+id/priceFl"
    app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
    android:id="@+id/priceFl"
    android:layout_width="0dp"
    app:layout_constraintHorizontal_weight="1"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/nameTv"
    app:layout_constraintTop_toTopOf="parent">
</FrameLayout>

I prefer using constraintStart and constraintEnd instead of constraintLeft and constraintRight, just a personal choice.

like image 44
tanni tanna Avatar answered Oct 11 '22 10:10

tanni tanna