Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give gravity for child views inside ConstraintLayout

I am working on ConstraintLayout, I can adjust the child views, but I can't align the Textview from left to right. Is there any way to align the Textview from left to right?

Here is the code:

<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:id="@+id/mylotslist_itemview"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/serialnumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:gravity="left"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:padding="@dimen/padding5"
        android:text="1"
        android:textColor="@color/black"
        android:textSize="@dimen/list_itemtextsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
    <TextView
        android:id="@+id/lotnumber"
        android:gravity="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:padding="@dimen/padding5"
        android:text="50"
        android:textColor="@color/black"
        android:textSize="@dimen/list_itemtextsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.08"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
    <TextView
        android:id="@+id/sellername"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="right"
        android:padding="@dimen/padding5"
        android:text="ffffffffff"
        android:textColor="@color/black"
  app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
         />

    <TextView
        android:id="@+id/lotstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:gravity="left"
        android:layout_marginTop="8dp"
        android:background="@color/lot_statusfalse"
        android:paddingBottom="@dimen/padding5"
        android:paddingLeft="@dimen/padding5"
        android:paddingRight="@dimen/padding5"
        android:paddingTop="@dimen/padding5"
        android:text="Alloted and Empty"
        android:textColor="@color/white"
        android:textSize="@dimen/list_itemtextsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.66"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
    <TextView
        android:id="@+id/createddate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="left"

        android:padding="@dimen/padding5"
        android:text="01-FEB-2018 10:30 AM"
        android:textColor="@color/black"
        android:textSize="@dimen/list_itemtextsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

I want my textview to be aligned like this. Is there any possible way to align like this?

enter image description here

like image 511
suraj s Avatar asked Jun 08 '18 06:06

suraj s


People also ask

How do I center a view in ConstraintLayout?

and you can right click in design mode and choose center. Show activity on this post. You can use layout_constraintCircle for center view inside ConstraintLayout . with constraintCircle to parent and zero radius you can make your view be center of parent.

Which attributes is the gravity of the view or Layout in its parent?

android:layout_gravity sets the gravity of the View or Layout relative to its parent.

What is difference between layout_gravity and gravity?

So in general android:layout_gravity attribute is used by child views to tell their parent how they want to be placed inside it, while android:gravity is used by the parent layout to tell the child views how they should be placed inside it.


1 Answers

Remove app:layout_constraintStart_toStartOf="parent" from all views except first one and also add app:layout_constraintStart_toEndOf = "@+id/your_left_views_id" for each view except first one.

like

<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:id="@+id/mylotslist_itemview"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/serialnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="left"
    android:padding="@dimen/padding5"
    android:text="1"
    android:textColor="@color/black"
    android:textSize="@dimen/list_itemtextsize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/lotnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="left"
    android:padding="@dimen/padding5"
    android:text="50"
    android:textColor="@color/black"
    android:textSize="@dimen/list_itemtextsize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.08"
    app:layout_constraintStart_toEndOf="@+id/serialnumber"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/sellername"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="right"
    android:padding="@dimen/padding5"
    android:text="ffffffffff"
    android:textColor="@color/black"
    app:layout_constraintStart_toEndOf="@+id/lotnumber"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/lotstatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@color/colorPrimary"
    android:gravity="left"
    android:paddingBottom="@dimen/padding5"
    android:paddingLeft="@dimen/padding5"
    android:paddingRight="@dimen/padding5"
    android:paddingTop="@dimen/padding5"
    android:text="Alloted and Empty"
    android:textColor="@color/white"

    android:textSize="@dimen/list_itemtextsize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.66"
    app:layout_constraintStart_toEndOf="@+id/sellername"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/createddate"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="left"
    android:padding="@dimen/padding5"
    android:text="01-FEB-2018 10:30 AM"
    android:textColor="@color/black"
    android:textSize="@dimen/list_itemtextsize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toEndOf="@+id/lotstatus"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

like image 51
Pallavi Tapkir Avatar answered Oct 19 '22 03:10

Pallavi Tapkir