Rohit Sharma
Rohit Sharma

Reputation: 13815

Linear Layout Wrapping TextView

I am designing a layout as following

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/messageText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Some really long text asd asd asd asd asd asd asd asd asd asd asd" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12:45 PM" />

</LinearLayout>

It gives a UI like the above...

Long Text Image

Which is perfect what i need. Long text is getting wrapped properly without pushing Button away from the screen as orientation is horizontal. button is also not stretching.

But if text is small. It looks like this

Short text image

What i want is Button should be aligned to the left end of the text like this

short correct text

I know as i put the layout weight to 1.

It consumes the whole area left after giving needed space to the button.

If i don't put that. and make the width to be wrap content. It works great for short text. but as text grows.

it pushes the button away from the screen.

Thats the problem. Hope i made my problem enough descriptive. But of course long to read. Painful of course. But will be really glad to know any pointer.

I am not reluctant to use relative layout as well.

Any help will be appreciated.

Thanks

Upvotes: 4

Views: 3171

Answers (3)

Avinash Kumar Pankaj
Avinash Kumar Pankaj

Reputation: 1720

i tested it on my device and working fine. try it

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >

<TextView
    android:id="@+id/messageText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="sdgdsgdhdhdfhdhgfffffffffffffffffffffffffffffffffffffffff" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="12:45 PM" />

</LinearLayout>

Upvotes: 1

Emil Adz
Emil Adz

Reputation: 41099

Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/messageText"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
     android:layout_weight="1.0"
    android:text="Some really long text asd asd asd asd asd asd asd asd asd asd asd![enter image description here][1]" />

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="12:45 PM" />

Upvotes: 1

krishna
krishna

Reputation: 998

Hope this works for you. please try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/messageText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Some really long text asd asd asd asd asd asd asd asd asd asd asd![enter image description here][1]" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="12:45 PM" />

I think you need to change android:layout_width="fill_parent" of linearlayout to android:layout_width="wrap_content"

Upvotes: 7

Related Questions