20

I created a LinearLayout like below with TextView. The Text is variable. If the Elements gets bigger than the Layout width it gets nasty. I want to have the text flow to a new line just one would do when writing a book. Is this possible?

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

<TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xx"
            android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText ...>

<TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xx"
            android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

edit

like this:

  1. textview1text edittext textview2
  2. textview2continuesin2ndrow

not like this:

  1. textview1text edittext textview2text
  2. textview1cont
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
user1324936
  • 2,187
  • 4
  • 36
  • 49
  • I'm not sure it fits your layout design, but you can probably solve the issue you're describing by setting (e.g. equal) weights to the three elements in the `LinearLayout` (and their widths to `0dp`). – MH. Apr 16 '12 at 19:24

3 Answers3

19

Try something like...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xx"
            android:textAppearance="?android:attr/textAppearanceSmall" />
        <EditText ...>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="xx"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
</LinearLayout>
Paamand
  • 626
  • 1
  • 6
  • 17
Squonk
  • 48,735
  • 19
  • 103
  • 135
7

you can use android:orientation="vertical" within linearlayout to get elements in different lines

Robert
  • 5,278
  • 43
  • 65
  • 115
vara prasad
  • 119
  • 1
  • 1
1

Try Something like this

<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.sushrut.recipedemo.MainActivity">

    <android.widget.LinearLayout 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="40dp"
        android:layout_marginTop="40dp"
        android:orientation="horizontal"
        tools:context="com.example.sushrut.recipedemo.MainActivity">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/cameraActionButton"
            android:layout_width="68dp"
            android:layout_height="68dp"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"

            app:fabSize="normal"
            app:srcCompat="@android:drawable/ic_menu_camera" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/choiceActionBtn"
            android:layout_width="68dp"
            android:layout_height="68dp"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"

            app:fabSize="normal"
            app:srcCompat="@android:drawable/ic_input_add" />

    </android.widget.LinearLayout>

    <android.widget.LinearLayout 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
        tools:context="com.example.sushrut.recipedemo.MainActivity">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/addToLibraryBtn"
            android:layout_width="68dp"
            android:layout_height="68dp"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:clickable="true"
            app:fabSize="normal"
            app:srcCompat="@android:drawable/ic_menu_save" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/recipeBrowseBtn"
            android:layout_width="68dp"
            android:layout_height="68dp"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:clickable="true"
            app:fabSize="normal"
            app:srcCompat="@android:drawable/btn_plus" />
    </android.widget.LinearLayout>

</android.widget.LinearLayout>
Sushrut Kanetkar
  • 363
  • 3
  • 13