skip to Main Content

I need to indent the same size in the second text view on the right as in the first one on the left. I tried to do through

android: text alignment = "textend"
android: layout_margin End = "20dp", 

now I have it done like this:

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/light_gray">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="TestTest"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAlignment="textEnd"
                android:layout_marginEnd="20dp"
                android:text="TestTest"/>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

but I think it should be done differently
enter image description here

2

Answers


  1. You can simply use layout_gravity and set it to end to push the 2nd textView towards the end.

               <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="TestTest1"
                    android:textStyle="bold" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="20dp"
                    android:layout_gravity="end"
                    android:text="TestTest1"
                    android:textStyle="bold" />
    

    Also note I have set the width to wrap_content as opposed to match_parent.
    Have also used a margin of 20 dp to offset the 2nd TextView from the edge of parent.

    TextAlignment attribute specifies the position of text inside the TextView and won’t work if the view is set to wrap_content

    Login or Signup to reply.
  2. Using android:weightSum You Can Separate.

            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/light_gray">
    
                <androidx.appcompat.widget.Toolbar
                   android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                    <androidx.appcompat.widget.LinearLayoutCompat
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:weightSum="2"
                        android:layout_marginEnd="@dimen/_20sdp"
                        android:layout_marginStart="@dimen/_20sdp"
                        android:orientation="horizontal">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="TextView"
                            android:layout_weight="1"
                            android:textSize="@dimen/_15sdp"
                            android:textStyle="bold"/>
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="TextView"
                            android:textAlignment="viewEnd"
                            android:layout_weight="1"
                            android:textSize="@dimen/_15sdp"
                            android:textStyle="bold"/>
    
                    </androidx.appcompat.widget.LinearLayoutCompat>
                </androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.AppBarLayout>
    

    Then For Second TextView Only Use android:textAlignment="viewEnd" Then Text Gravity Goes To Width End

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search