Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display 2 textviews in the same line in Android

I have my layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
     android:id="@+id/titlename"  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="@string/HostName"
 android:layout_weight="0"

/>
<TextView
     android:id="@+id/name"  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
 android:layout_weight="0"
/>
</LinearLayout>

when i execute the above one, my output is like below:

enter image description here

But My Requirement is to get my output below:

|   text1:    text2    |

Could any one help?

like image 838
String Avatar asked Apr 23 '13 18:04

String


2 Answers

Use Following Code

enter image description here

Updated :

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal"
   android:weightSum="1" >
<TextView
    android:id="@+id/titlename"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="hi" />
<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="2hi2" />
 </LinearLayout>
like image 182
Ronak Mehta Avatar answered Oct 15 '22 03:10

Ronak Mehta


Have android:layout_width as 0dp. Then set 0.5 weights for each of the text view's. Each of the textview will fill half the width of the parent Layout. Something like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
     android:id="@+id/titlename"  
android:layout_width="0dp"
android:layout_height="wrap_content" 
android:text="@string/HostName"
 android:layout_weight="0.5"

/>
<TextView
     android:id="@+id/name"  
android:layout_width="0dp"
android:layout_height="wrap_content" 
 android:layout_weight="0.5"
/>
</LinearLayout>
like image 31
Shobhit Puri Avatar answered Oct 15 '22 02:10

Shobhit Puri