How to display 2 textviews in the same line in Android
Asked Answered
D

9

8

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?

Downpipe answered 23/4, 2013 at 18:7 Comment(1)
Did you try android:layout_weight="0.5" with both the text views ? This would give half width to both?Diaper
S
27

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>
Schatz answered 23/4, 2013 at 18:9 Comment(0)
D
3

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>
Diaper answered 23/4, 2013 at 18:21 Comment(0)
G
1

Create a LinearLayout enclosing the 2 TextViews.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnLogin"
        android:layout_below="@id/etPassword"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:text="Login"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnLogin"
        android:layout_marginTop="10dp"
        android:gravity="center">

        <TextView
            android:id="@+id/tvIAlwaysHaveAccount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnLogin"
            android:text="I always have an account?" />

        <TextView
            android:id="@+id/tvForgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/tvIAlwaysHaveAccount"
            android:layout_marginLeft="15dp"
            android:text="Forgot password?" />

    </LinearLayout>


</RelativeLayout>

and your show the same:"

enter image description here

Gidgetgie answered 15/12, 2020 at 1:49 Comment(0)
C
0

I would take out the weight attribute. That's the only thing I can see that would be screwing it up. Also note that fill_parent is deprecated and you should use match_parent instead

Or, if you need the weight for some reason then change each to 1 if you want them to each take up half the screen.

Also, if using weight then you should set android:layout_width="0dp"

Celebrity answered 23/4, 2013 at 18:10 Comment(0)
B
0

Set the android:orientation="horizontal" for your LinearLayout.

Beacon answered 23/4, 2013 at 18:11 Comment(0)
T
0

Update your layout like below, you don't want to include the weight=0 attribute unless you set a weightSum on the parent LinearLayout. Plus weights and weightSum doesn't really fit what you're looking for.

<?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"

        />
    <TextView
         android:id="@+id/name"  
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
        />
</LinearLayout>
Tutto answered 23/4, 2013 at 18:13 Comment(0)
S
0

You could try to use a RelativeLayout

With that you are able to set alignParentTop="true" at titlename element and set name alignTo="@id/titlename" and toRightOf="@id/titlename" at name element

Selfeducated answered 23/4, 2013 at 18:14 Comment(0)
L
0

Actually your original code works perfectly in my ADT. Did you change text size or line padding in Java code which updates the second textview?

Levulose answered 23/4, 2013 at 18:26 Comment(0)
P
0
<?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="wrap_content"
     android:layout_height="wrap_content" 
     android:text="@string/HostName"
/>
<TextView
     android:id="@+id/name"  
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
/>
</LinearLayout>
Placida answered 29/5, 2017 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.