// Try this way,hope this will help you...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtNameLable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="0.25"
android:textSize="16sp"
android:text="Name : "/>
<TextView
android:id="@+id/txtNameValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:layout_marginLeft="3dp"
android:textSize="16sp"
android:text="Mr.ABC"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/txtAddressLable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="0.25"
android:textSize="16sp"
android:text="Address : "/>
<TextView
android:id="@+id/txtAddressValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:layout_marginLeft="3dp"
android:textSize="16sp"
android:text="Flat A, 25/F, Block 2,\nUSA...."/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/txtGenderLable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="0.25"
android:textSize="16sp"
android:text="Gender : "/>
<TextView
android:id="@+id/txtGenderValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:layout_marginLeft="3dp"
android:textSize="16sp"
android:text="Male"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/txtHobbiesLable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="0.25"
android:textSize="16sp"
android:text="Hobbies : "/>
<TextView
android:id="@+id/txtHobbiesValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:layout_marginLeft="3dp"
android:textSize="16sp"
android:text="Football,Baseball \n......"/>
</LinearLayout>
</LinearLayout>
How to align the text inside the android text view
-
23-07-2023 - |
Question
For example, I have a ListView , and for each row that data is like this
Name: Mr.ABC
Address: Flat A, 25/F, Block 2,
USA....
Gender: Male
Hobbies: Football, Baseball.....
.....
And the above is how it currently look likes, I use 4 text view to represent each data
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:orientation="vertical"
android:paddingLeft="5dp" >
<TextView
android:id="@+id/resultName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/resultLang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/resultType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/resultOrg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/resultYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
The problem is how can I align the text like this?
Name: Mr.ABC
Address: Flat A, 25/F, Block 2,
USA....
Gender: Male
Hobbies: Football, Baseball.....
.....
Thanks for helping
Updated
<TableLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:paddingLeft="5dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_weight="1"
android:text="@string/search_name"
android:textSize="16sp" />
<TextView
android:id="@+id/resultName"
android:layout_weight="1"
android:textSize="16sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_weight="1"
android:text="@string/search_lang"
android:textSize="16sp" />
<TextView
android:id="@+id/resultLang"
android:layout_weight="1"
android:textSize="16sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_weight="1"
android:text="@string/search_type"
android:textSize="16sp" />
<TextView
android:id="@+id/resultType"
android:layout_weight="1"
android:textSize="16sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_weight="1"
android:text="@string/search_org"
android:textSize="16sp" />
<TextView
android:id="@+id/resultOrg"
android:layout_weight="1"
android:textSize="16sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_weight="1"
android:text="@string/search_yr"
android:textSize="16sp" />
<TextView
android:id="@+id/resultYear"
android:layout_weight="1"
android:textSize="16sp" />
</TableRow>
</TableLayout>
The above is updated code but the problem is , since it is listview and I found that the left side width is not equal . It is something like e.g.
Name: Mr.ABC
Address: Flat A, 25/F, Block 2,
USA....
Gender: Male
Hobbies: Football, Baseball.....
.....
Name: Mr.Asfsafasfdfsd
Address: Flat V, 25/F, Block 2,
USA....
Gender: Female
Hobbies: Basketball.....
.....
Name: Mr.WQWRWEE
Address: Flat V, 25/F, Block 2,
USA....
Gender: Male
Hobbies: Baseball.....
.....
Solution
OTHER TIPS
Basically what you want is a Table that contains rows with two columns. I am guessing, you should use the TableLayout to lay out the twi TextViews.
Good Luck
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow