Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 477
Rohit Sharma Avatar asked Feb 17 '23 21:02

Rohit Sharma


1 Answers

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"

like image 101
krishna Avatar answered Feb 28 '23 05:02

krishna