Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GUI - How to force a button to the next line?

Sorry if I'm asking such a basic question. I have tried to google all morning, and read through the documentations at developer.android.com, but I couldn't find a solution.

In short, I would like to make an interface that looks like this:

enter image description here

This is what I have so far:

enter image description here

Now, I'm trying to bring the 2 buttons to the next line, but I still have no luck with that. If I change the android:orientation="horizontal" to "vertical", everything was aligned vertically. I tried to bring the line android:orientation="vertical" inside the Button tag, but that didn't seem to work.

What else can I try? Would somebody give me a few pointers? (I'm new at Android development and all these XML stuff).


Below is my XML code:

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

<EditText android:id="@+id/txt_Input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:hint="@string/txt_Input"

        android:layout_weight="4"                      
/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_Send" 

        android:layout_weight="1"

        android:onClick="sendMessage"
/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_Send" 

        android:layout_weight="1"

        android:onClick="sendMessage"
/>

</LinearLayout>
like image 663
TATN Avatar asked Oct 23 '12 20:10

TATN


People also ask

How to set LinearLayout in android?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How to give weight to LinearLayout in android?

Weight can only be used in LinearLayout . If the orientation of linearlayout is Vertical, then use android:layout_height="0dp" and if the orientation is horizontal, then use android:layout_width = "0dp" . It'll work perfectly.

What is the use of layout_ weight in android?

In a nutshell, layout_weight specifies how much of the extra space in the layout to be allocated to the View. LinearLayout supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view.


1 Answers

Just use RelativeLayout, and your buttons should look something like this:

<Button
    android:id="@+id/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_Send" 
    android:onClick="sendMessage"
    android:layout_below="@+id/txt_Input"
    android:layout_alignParentLeft="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_Send" 
    android:onClick="sendMessage"
    android:layout_below="@+id/txt_Input"
    android:layout_toRightOf="@+id/send"/>
like image 189
meh Avatar answered Sep 30 '22 23:09

meh