The screen of my application (https://i.sstatic.net/xFozp.png)
As seen in the image I have aligned textviews horizontally. When the product name is longer the name is displayed in 2 lines in the textview. But when the number of lines in textview increases the starting lines of two textviews are not horizontally aligned. I want the starting lines of textviews to be horizontally aligned. Currently I am using LINEAR LAYOUT. Should I change the layout? Can anyone give the code to be written. Thus even though 2 textviews are besides each other (as seen in the image) their starting( first line) should be in same horizontal line.
My code is as follows-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@drawable/b22"
android:gravity="center|left"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="15dp"
android:maxLines="3" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:text="TextView"
android:textColor="@android:color/black"
android:maxLines="3" />
</LinearLayout>
1. Remove attribute android:gravity="center|left"
from LinearLayout
.
2. Remove attribute android:layout_gravity="right|center"
from textView2
and change its width as android:layout_width="match_parent"
.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Product Name -"
android:textColor="@android:color/black"
android:textSize="15dp"
android:maxLines="3" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is Liberty Agile Pension Preserver Product"
android:textColor="@android:color/black"
android:maxLines="3" />
</LinearLayout>
OUTPUT:
For your design, its best to use a single RelativeLayout
and put all of your TextViews
inside it.
Here is an example:
<?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:layout_margin="10dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Product Name -"
android:textColor="@android:color/black"
android:textSize="15dp"
android:maxLines="3" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"
android:layout_alignTop="@id/textView1"
android:text="This is Liberty Agile Pension Preserver Product"
android:textColor="@android:color/black"
android:maxLines="3" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:text="Number of Senarios Executed -"
android:textColor="@android:color/black"
android:textSize="15dp"
android:maxLines="3" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView3"
android:layout_alignTop="@id/textView3"
android:text="400"
android:textColor="@android:color/black"
android:maxLines="3" />
</RelativeLayout>
OUTPUT:
Hope this will help~