Search code examples
androiduitextviewbottom-sheetmargins

Align mutliple textview to both center and similar left alignment for all screens


enter image description here

I am working on a bottom sheet dialog and was wondering how to align all 3 textviews to the center as well as align them with the same left margin. Please note that there is also an imageview left to the textview for all 3 rows(Here 3 rows are rename, share and delete). gravity center makes them align to center but their left margin won't be same. I want their left margin to also be same

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/btmsheet"
    android:background="@color/white"
    android:layout_marginLeft="@dimen/pad_5dp"
    android:layout_marginRight="@dimen/pad_5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:id="@+id/dragView"
        android:layout_marginTop="@dimen/pad_10dp">

        <View
            android:layout_width="@dimen/pad_50dp"
            android:layout_height="@dimen/pad_5dp"
            android:background="@color/grey_color" />
    </LinearLayout>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="15dp">

            <TextView
                android:id="@+id/bottom_sheet_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="@dimen/text_20sp"
                android:textColor="@color/black"
                android:text=""
                tools:text="RB000019.JPG"
                android:textStyle="bold"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#E5E5E5" />

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/lyt_duplicate"
            android:orientation="horizontal"
            android:padding="15dp"
       >

            <TextView
                android:id="@+id/bottom_sheet_rename"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableStart="@drawable/edit"
                android:drawablePadding="@dimen/pad_15dp"
                android:paddingStart="@dimen/pad_20dp"
                android:text="@string/rename_project_name"
                android:textColor="@color/black"
                android:textSize="@dimen/text_16sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </android.support.constraint.ConstraintLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#E5E5E5" />

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/lyt_rename"
            android:orientation="horizontal"
            android:padding="15dp"
            android:gravity="center_horizontal|center_vertical">

            <TextView
                android:id="@+id/bottom_sheet_share"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_16sp"
                android:textColor="@color/black"
                android:text="@string/share_project"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:drawableStart="@drawable/ic_share"
                android:drawablePadding="@dimen/pad_15dp"/>
        </android.support.constraint.ConstraintLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="#E5E5E5" />

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:orientation="horizontal"
            android:padding="15dp"
            android:id="@+id/lyt_move"
            android:gravity="center_horizontal|center_vertical">


            <TextView
                android:id="@+id/bottom_sheet_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_16sp"
                android:textColor="@color/black"
                android:text="@string/delete_project"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:drawableStart="@drawable/delete"
                android:layout_marginStart="@dimen/pad_5dp"
                android:drawablePadding="@dimen/pad_20dp"
               />
        </android.support.constraint.ConstraintLayout>


    </LinearLayout>




</LinearLayout>

Solution

  • There are many ways to accomplish the above.

    1. You can use vertical LinearLayout and have 3 horizontal LinearLayout's inside, wrapping icon and text view.
    2. You could also use ConstraintLayout - you'd then have smaller depth of your layout tree.
    3. You could use RecyclerView where each item would be horizontal LinearLayout, or ConstraintLayout - representing row

    Updated as per chat with user:

    This is to give you an idea:

    <?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">
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.4" />
    
        <ImageView
            android:id="@+id/renameIcon"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_margin="16dp"
            android:background="#ff0000"
            app:layout_constraintEnd_toStartOf="@+id/guide"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/renameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rename Project"
            app:layout_constraintBottom_toBottomOf="@+id/renameIcon"
            app:layout_constraintStart_toEndOf="@+id/guide"
            app:layout_constraintTop_toTopOf="@+id/renameIcon" />
    
        <ImageView
            android:id="@+id/shareProjectIcon"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_margin="16dp"
            android:background="#ff0000"
            app:layout_constraintEnd_toStartOf="@+id/guide"
            app:layout_constraintTop_toBottomOf="@+id/renameIcon" />
    
        <TextView
            android:id="@+id/shareProject"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Share Project"
            app:layout_constraintBottom_toBottomOf="@+id/shareProjectIcon"
            app:layout_constraintStart_toEndOf="@+id/guide"
            app:layout_constraintTop_toTopOf="@+id/shareProjectIcon" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Here is how it's going to look:

    enter image description here