Ricardo Filipe
Ricardo Filipe

Reputation: 445

Insert a linearLayout inside another linearLayout

I'm trying to insert a LinearLayout inside another LinearLayout. I don't know if what I am doing is right or not. I need to try it this way, without using inflation.

      LinearLayout address2;
      address2 = new LinearLayout(this);
      address2 = (LinearLayout)findViewById(R.id.sfsp2_layout);

      LinearLayout teste3 = (LinearLayout)findViewById(R.id.se_contentAdressPostal);

      LinearLayout teste4 = (LinearLayout)teste3.findViewWithTag("teste");
      teste4.addView(address2);

LinearLayout teste3

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/se_contentAdressPostal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:background="@drawable/background_tile_address_postal"
    android:orientation="vertical" >

    <include
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/socio_form_structured_postal" />

LinearLayout teste4

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sfsp_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_tile"
    android:orientation="vertical"
    android:tag="teste" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/sfsp_layout_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="49dp"
        android:layout_gravity="right"
        android:background="@drawable/background_tile"
        android:orientation="horizontal"
        android:tag="teste" >
        <Button
            android:id="@+id/sfsp_btStructuredPostal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="2dp"
            android:hint="@string/sfsp_btStructuredPostal" /> .......

LinearLayour address2 ( The layout that i need to insert in layout4)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sfsp2_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_tile"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/sfsp2_etStructuredPostalApartado"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="65dp"
        android:layout_marginRight="55dp"
        android:layout_marginTop="2dp"
        android:layout_weight="0.14"
        android:ems="10"
        android:hint="@string/sfsp2_etStructuredPostalApartado"
        android:inputType="textMultiLine"
        android:scrollHorizontally="false" >

The LinearLayout "teste4" is inside "teste3". I need to insert LinearLayout "address2" inside "teste4". Can you tell me what I'm doing wrong?.

Upvotes: 1

Views: 1818

Answers (1)

ikh
ikh

Reputation: 2436

I think there is some misunderstanding here about how the findViewById works. I'll try to explain it below. Hope this helps, let me know if it doesn't.

There are two possible findViewById() that can be used.

  1. Activity.findViewById(): this can be used to find a view in the current activity's main content view -- that is, the view that was set using setContentView(). It cannot be used for any other purpose. For example, it cannot find a view from any other layout file -- the function simply would have no way of knowing how to find it. As far as I can tell, this is the function you're trying to use. If the last XML file you have is not the main layout, then the line address2 = (LinearLayout)findViewById(R.id.sfsp2_layout) will fail.

  2. View.findViewById(): this function can be used to find a view that is contained in some other view. E.g. if you have view1 that contains view2, and view2 has ID some_id, then you can find view2 by calling view1.findViewById(R.id.some_id). In order for this to work, view1 has to be fully initialized. E.g. if view1's description is in XML, it has to be fully inflated first.

In essence, if you want to work with a view that is described in a separate XML file, you have to inflate it first. I don't think there is a way around it.

Upvotes: 2

Related Questions