Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout inside LinearLayout?

I was wondering what I am doing wrong here, why does surrounding these buttons in a RelativeLayout cause an error? I haven't edited layouts much, can I not just stick in a Relative or Linear layout like that?

If I don't surround the buttons in a layout they appear beneath each other, I am just trying to experiment in having them beside each other horizontally.

I think the way I have done this whole layout is bad because vertically it looks quite squished.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


   <LinearLayout android:id="@+id/topButtons" 
    android:layout_margin="4dip"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:id="@+id/deviceConnect"
        android:layout_margin="8dip"
        android:layout_weight="3"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="Connect2"/>
    <LinearLayout android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout android:orientation="horizontal"
            android:layout_margin="8dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:text="Connected Adapter:"
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Spinner android:id="@+id/deviceSpinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"/>
            <TextView android:id="@+id/currentSettings" 
                android:layout_marginLeft="8dip"
                android:text="Current Settings: Not Connected"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"/>
        </LinearLayout>
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Baud:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/baudSpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Data:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/dataSpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Parity:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/paritySpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Stop:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/stopSpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <Button android:id="@+id/updateSettings"
        android:layout_margin="8dip"
        android:layout_weight="3"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="Update\nSettings"/>
</LinearLayout>
<RelativeLayout>
   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 
    </RelativeLayout>

<jackpal.androidterm.emulatorview.EmulatorView
    android:id="@+id/emulatorView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:focusable="true"
    android:focusableInTouchMode="true"

    />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/term_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:imeOptions="actionNone|flagNoExtractUi"
        android:inputType="text|textImeMultiLine" />

    <Button
        android:id="@+id/term_entry_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="@string/entry_send" />

</LinearLayout>

like image 798
Paul Avatar asked Jan 08 '13 13:01

Paul


People also ask

What is the difference between RelativeLayout and LinearLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Which has better performance LinearLayout or RelativeLayout?

So Linear Layout should preferred over Relative Layout! Also if you use weight_sum attribute within Linear Layout it would again “measure the child twice”. So avoid using this attribute as much as possible.

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.


1 Answers

As others have pointed out already, you need to define a layout_width and layout_height for your RelativeLayout. This should fix your error, unless there are other issues you have. But I thought it was worth noting what the DOCS say about layouts

A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.

You have many nested LinearLayouts so you may consider using a RelativeLayout for better performance and readability. You can also use android:orientation="horizontal" in a nested LinearLayout to get your buttons next to each other. But, again, a RelativeLayout may be better in your situation instead of having all of the nested layouts.

like image 136
codeMagic Avatar answered Sep 28 '22 22:09

codeMagic