3

I try to create 2 rows of buttons in android layout.xml file. The first row is left-aligned, the second row is center-aligned.

Here is what I did, but I end up getting 1 row of buttons. Can you please tell me what am I doing wrong?

enter code here
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="fill_parent">
 <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|left"
        >
         <Button 
          android:id="@+id/btn1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
     <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|center"
        >
         <Button 
          android:id="@+id/btn2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
       <Button 
          android:id="@+id/btn3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
</FrameLayout>
</pre>

4 Answers 4

5

FrameLayout is wrong for this task. Use LinearLayout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="fill_parent">
 <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|left"
        >
         <Button
          android:id="@+id/btn1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
     <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_gravity="bottom|center"
        >
         <Button
          android:id="@+id/btn2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
       <Button
          android:id="@+id/btn3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
        </LinearLayout>
</LinearLayout>

for explanation on what FrameLayout is for go here: http://developer.android.com/reference/android/widget/FrameLayout.html

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Tablelayout for displaying button in the form of rows.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:id="@+id/root"
      android:stretchColumns="*"
      android:background="#000000">
  <TableRow android:layout_margin="0dip"
         android:id="@+id/first_row">
    <Button android:id="@+id/button01" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button02" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button03" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button04" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
    <Button android:id="@+id/button05" 
          android:layout_width="0dip"
          android:layout_weight="1"
          android:padding="15dip" />
  </TableRow>
</TableLayout>

Comments

2

I would recommend against nesting LinearLayouts like that. It's not productive, very inefficient and hinders the ability of Android layouts to expand in some cases and UI changes.

And besides, it's very CPU intensive! Don't do it and read this:

http://developer.android.com/training/improving-layouts/optimizing-layout.html

Comments

0

You could also use just one RelativeLayout instead of all of your layouts. I've read many reports that RelativeLayout uses less resources than LinearLayout.

2 Comments

Relative Layout is by far the most expensive layout mechanism. Source, a Google Engineer: plus.google.com/+KirillGrouchnikov/posts/fFfm7jKn7q5
Your linked "article" (more like sentence) is talking about layouts within an adapter. OP does not require this to be in an adapter. Just because the words come out of the holy gods' mouths, doesn't mean there isn't more going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.