Search code examples
javaandroidlayouttextviewandroid-gridlayout

Align vertical text views of different lengths to same right edge using Grid Layout


enter image description here

I want to make the detail part of this image using Grid layout but the problem i am facing is that my text views are not ending below each other when they are of different lengths.My text views starting point is same ending point is different but in this image layout they are ending below each other. For example the value of Vol has more digits than value of low but both have same end point. How do i achieve this using grid layout? Here's my code

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="4"
android:padding="8dp"
android:orientation="horizontal"
android:background="@drawable/toolbar_background">

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:text="Open"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:text="36.40"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="10dp"
    android:text="MktCap"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:text="33.39B"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginTop="10dp"
    android:text="High"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:text="36.95"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text="52WkHigh"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:text="42.31"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginTop="10dp"
    android:text="Low"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:text="36.08"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text="52WkLow"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:text="26.15"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginTop="10dp"
    android:text="Vol"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:text="19.66M"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text="P/E"/>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:text="1.25"/>


Solution

  • use

    android:layout_gravity="right"
    

    in all textview s that you want to ending below each other . also you may add this

     android:gravity="right"