0

enter image description here

Hi everyone, I have a issue here, I have a linear layout and I want to add buttons in horizontal but I also need it auto break the line if button touch the linear layout edge. I coded my button (not XML) and set it "wrapcontent" (Text inside). My english is not very good so I draw a image here.

Thank for you reading!

Tai Tran
  • 1,406
  • 3
  • 15
  • 27
  • @ValentinoRu, I make a button by code, set text to it and wrap content... – Tai Tran Dec 01 '12 at 02:10
  • 2
    You could use a `RelativeLayout`, get the window width at runtime and align with relative positioning based on button width vs screen width. – jnthnjns Dec 01 '12 at 02:12
  • 2
    What you are looking for is called a `FlowLayout`, and it is not in the framework directly. However, Romain Guy from the Android team has created a sample implementation including a video where he live codes and explains the example. You can see this in his SO answer here: http://stackoverflow.com/a/4474533/246461 – devunwired Dec 01 '12 at 04:41
  • @Devunwired: You make me a day. Thank you so much.. – Tai Tran Dec 01 '12 at 05:55

1 Answers1

2

if you know the sizes of your buttons before, then you can put Layouts into Layouts. See this example that represents your image:

<LinearLayout
    android:id="@+id/outerLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout 
        android:id="@+id/innerLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button 
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
        <Button 
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/innerLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button 
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
        <Button 
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
    </LinearLayout>
    <Button
       android:id="@+id/button5"
       android:layout_width="match_parent"
       android:layout_weight="5"
       android:layout_height="wrap_content"
       /> 
</LinearLayout>

As you can see, with the layout_weight attribute you can manipulate the width of a button. This is possible too by setting a concrete size in layout_width except match_parent.

The same thing if you add Views from Code. However I prefer explaining Android layouts in XML, as you got a way better overview.

Valentino Ru
  • 4,964
  • 12
  • 43
  • 78