-1

I want to display data of emi calculation in listview, i am displaying it but the problem is it's gravity is not same for every textview in row, i mean i set gravity right for all but it's not looking in linear as in screenshot enter image description here

I want every textview in right aligned and should be liner column vise.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >

<TextView
    android:id="@+id/emi_calc_row_tv_year"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Year"
    android:layout_weight="0.2"
    android:gravity="center_horizontal"
    android:layout_gravity="right" />


<TextView
    android:id="@+id/emi_calc_row_tv_principal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Principal"
    android:layout_weight="0.2"
    android:gravity="center_horizontal" 
    android:layout_gravity="right" />

<TextView
    android:id="@+id/emi_calc_row_tv_interest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Interest"
    android:layout_weight="0.2"
    android:gravity="center_horizontal"
    android:layout_gravity="right"  />

<TextView
    android:id="@+id/emi_calc_row_tv_total"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Total\n(A+B)"
    android:layout_weight="0.2"
    android:layout_gravity="right"
    android:gravity="center_horizontal"  />

<TextView
    android:id="@+id/emi_calc_row_tv_balance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Balance"
    android:layout_weight="0.2"
    android:gravity="center_horizontal"
    android:layout_gravity="right"  />

</LinearLayout>
Rajesh Panchal
  • 1,140
  • 3
  • 20
  • 39

2 Answers2

1

Use android:gravity="right" instead of android:gravity="center_horizontal".

And change the width from android:layout_width="wrap_content" to android:layout_width="0dp".

This is an example from your code, i've tested it and it works.

<TextView
        android:id="@+id/emi_calc_row_tv_year"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Year"
        android:layout_weight="0.2"
        android:gravity="right" />
Rami
  • 7,879
  • 12
  • 36
  • 66
0

Firstly, I think you are confuse the define of two attribute android:gravity and android:layout_gravity.

  • android:gravity sets the gravity of the content of the View its used on.
  • android:layout_gravity sets the gravity of the View or Layout in its parent.

So, you need change each TextView to

<TextView
    android:id="@+id/emi_calc_row_tv_balance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Balance"
    android:layout_weight="0.2"
    android:gravity="right"  />
Pi Vinci
  • 381
  • 3
  • 10