Reputation: 451
I'm trying to achieve following output:
But what I get is:
I can't seem to figure out how to center the two TextViews
. The code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/chat_room_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Progress bar -->
<ProgressBar
android:id="@+id/chat_room_image_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxHeight="40dp"
android:minHeight="40dp"
android:minWidth="40dp"
android:maxWidth="40dp"
android:theme="@style/progressColor"
android:layout_margin="15dp" />
<!-- user's avatar image -->
<ImageView
android:id="@+id/chat_room_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/chat_room_title_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@id/chat_room_image_layout"
android:layout_toEndOf="@id/chat_room_image_layout">
<!-- user names -->
<TextView
android:id="@+id/chat_room_users_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/blackColor"
android:includeFontPadding="false"
android:layout_marginTop="10dp"/>
<!-- last message -->
<TextView
android:id="@+id/chat_room_last_message_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:text="aaaaaaaaaaaaaaaaaaaaa"
android:includeFontPadding="false"
android:textColor="@color/blackColor"
android:layout_marginBottom="10dp" />
</LinearLayout>
</RelativeLayout>
The avatar is being set dynamically with the height and weight of 50dp
. I tired to follow this topic but without any succsus. How can I center those two textviews?
Upvotes: 0
Views: 272
Reputation: 13019
In your layout file, there are two RelativeLayout
s as well as one LinearLayout
. Having many ViewGroup
s may be bad for performance, especially if you want to use the layout for a row of some really long list. So I'd like to suggest another option: with a ConstraintLayout
you get rid of the nested ViewGroup
s
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground">
<!-- user's avatar image -->
<ImageView
android:id="@+id/chat_room_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline"/>
<!-- Progress bar -->
<ProgressBar
android:id="@+id/chat_room_image_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline"
android:gravity="center"
android:maxHeight="40dp"
android:minHeight="40dp"
android:minWidth="40dp"
android:maxWidth="40dp"
android:layout_margin="15dp" />
<!-- invisible helper: 15dp + 50dp + 15dp = 80dp -->
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="80dp"/>
<!-- user names -->
<TextView
android:id="@+id/chat_room_users_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/chat_room_last_message_text"
app:layout_constraintStart_toEndOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="packed"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/blackColor"
android:includeFontPadding="false"
android:layout_marginTop="10dp"/>
<!-- last message -->
<TextView
android:id="@+id/chat_room_last_message_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
app:layout_constraintTop_toBottomOf="@+id/chat_room_users_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="15sp"
android:text="aaaaaaaaaaaaaaaaaaaaa"
android:includeFontPadding="false"
android:textColor="@color/blackColor"
android:layout_marginBottom="10dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
For more information on ConstraintLayout
: Build a Responsive UI with ConstraintLayout
Upvotes: 0
Reputation: 364095
You can use:
<LinearLayout
android:layout_height="wrap_content"
android:minHeight="56dp"
android:layout_width="match_parent">
<!-- user's avatar image -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingStart="15dp"
android:paddingEnd="15dp"/>
<!-- Progress bar -->
<ProgressBar
android:id="@+id/chat_room_image_progressbar"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
..>
<LinearLayout
android:id="@+id/chat_room_title_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<!-- user names -->
<TextView
android:id="@+id/chat_room_users_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
.../>
<!-- last message -->
<TextView
android:id="@+id/chat_room_last_message_text"
android:text="text2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
... />
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 364
I added
android:layout_centerVertical="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/chat_room_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Progress bar -->
<ProgressBar
android:id="@+id/chat_room_image_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxHeight="40dp"
android:minHeight="40dp"
android:minWidth="40dp"
android:maxWidth="40dp"
android:theme="@style/progressColor"
android:layout_margin="15dp" />
<!-- user's avatar image -->
<ImageView
android:id="@+id/chat_room_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/chat_room_title_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
***android:layout_centerVertical="true"***
android:layout_toRightOf="@id/chat_room_image_layout"
android:layout_toEndOf="@id/chat_room_image_layout">
<!-- user names -->
<TextView
android:id="@+id/chat_room_users_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/blackColor"
android:includeFontPadding="false"
android:layout_marginTop="10dp"/>
<!-- last message -->
<TextView
android:id="@+id/chat_room_last_message_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:text="aaaaaaaaaaaaaaaaaaaaa"
android:includeFontPadding="false"
android:textColor="@color/blackColor"
android:layout_marginBottom="10dp" />
</LinearLayout>
</RelativeLayout>
Upvotes: 0