Reputation: 2643
I'm trying to center two views vertically on a third view.
<ImageView
android:id="@+id/vh_buisnesspost_buisness_preview"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_margin="16dp"
android:background="@drawable/background_circle_preview_border"/>
<TextView
android:id="@+id/vh_buisnesspost_buisness_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buisness Name"
android:textStyle="bold"
android:textColor="@color/colorBlackFont"
app:layout_constraintStart_toEndOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintTop_toTopOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintBottom_toTopOf="@+id/vh_buisnesspost_date_posted"
android:layout_marginStart="16dp"/>
<TextView
android:id="@+id/vh_buisnesspost_date_posted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="2 mins ago"
app:layout_constraintStart_toEndOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintBottom_toBottomOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintTop_toBottomOf="@+id/vh_buisnesspost_buisness_name"
android:layout_marginStart="16dp"/>
<TextView
android:id="@+id/vh_buisnesspost_post_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text=" \u2022 Restaurant"
app:layout_constraintStart_toEndOf="@+id/vh_buisnesspost_date_posted"
app:layout_constraintBottom_toBottomOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintTop_toBottomOf="@+id/vh_buisnesspost_buisness_name"/>
However when I do this, it leaves an unwanted space between the two views.
What can I do to center these two views, to the third view?
Upvotes: 2
Views: 1079
Reputation: 6346
Add app:layout_constraintVertical_chainStyle="packed"
to the first TextView to change the style of the chain. The default style is spread
which will leave a space between the chained views to spread them equally. The packed
style will pack them together with a default bias of 0.5 which is what you want.
EDIT for the updated question:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/vh_buisnesspost_buisness_preview"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_margin="16dp"
android:background="@drawable/background_circle_preview_border"/>
<TextView
android:id="@+id/vh_buisnesspost_buisness_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buisness Name"
android:textStyle="bold"
android:textColor="@color/colorBlackFont"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintStart_toEndOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintTop_toTopOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintBottom_toTopOf="@+id/vh_buisnesspost_date_posted"
android:layout_marginStart="16dp"/>
<TextView
android:id="@+id/vh_buisnesspost_date_posted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="2 mins ago"
app:layout_constraintStart_toEndOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintBottom_toBottomOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintTop_toBottomOf="@+id/vh_buisnesspost_buisness_name"
android:layout_marginStart="16dp"/>
<TextView
android:id="@+id/vh_buisnesspost_post_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text=" \u2022 Restaurant"
app:layout_constraintStart_toEndOf="@+id/vh_buisnesspost_date_posted"
app:layout_constraintBottom_toBottomOf="@+id/vh_buisnesspost_date_posted"
app:layout_constraintTop_toTopOf="@+id/vh_buisnesspost_date_posted"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 4
Reputation: 3320
try this sample it dosn't require other ViewGroup
only ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="16dp"
android:background="@drawable/background_circle_preview_border"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Buisness Name"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintStart_toEndOf="@+id/image"
app:layout_constraintTop_toTopOf="@id/image"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 mins ago"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/image"
app:layout_constraintStart_toStartOf="@id/textView1"
app:layout_constraintTop_toBottomOf="@id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 346
Wrapping the two text view inside a linear layout does the trick
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintStart_toEndOf="@+id/vh_buisnesspost_buisness_preview"
app:layout_constraintTop_toTopOf="@+id/vh_buisnesspost_buisness_preview">
<TextView
android:id="@+id/vh_buisnesspost_buisness_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Buisness Name"
android:textColor="@color/colorAccent"
android:textStyle="bold"/>
<TextView
android:id="@+id/vh_buisnesspost_date_posted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="2 mins ago"
android:textSize="12sp"/>
</LinearLayout>
Upvotes: 0