4

In need to get a LinearLayout that isn't located in the main xml file (the one that i set in setContentView()). So in write this code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout main = (LinearLayout) findViewById(R.id.main1);

    LinearLayout oggetto = (LinearLayout) findViewById(R.id.element1);

    main.addView(oggetto);
}

The xml file where element1 is written is:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/element1"
            android:layout_width="fill_parent"
            android:orientation="horizontal" 
            android:layout_height="90px"
            android:background="#000000">

            <LinearLayout
                android:id="@+id/linearLayoutLeftBar"
                android:layout_width="10px"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:background="#7FFF00" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayoutRightContent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:background="#EEEEEE" >

                <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="fill_parent"
                    android:layout_height="45px"
                    android:orientation="horizontal"
                    android:background="#EEEEEE" >

                    <LinearLayout
                        android:id="@+id/linearLayoutTxtView1"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:background="#EEEEEE"
                        android:layout_gravity="right" >

                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:text="TextBox1"
                            android:layout_marginLeft="5px"
                            android:textColor="#000000"
                            android:gravity="center" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linearLayoutImgView"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:gravity="right|center" >

                        <ImageView
                            android:id="@+id/imageView1"
                            android:layout_width="16px"
                            android:layout_height="16px"
                            android:layout_marginRight="16px"
                            android:src="@drawable/ic_launcher"
                            android:background="#FF0000"/>

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="fill_parent"
                    android:layout_height="45px"
                    android:orientation="horizontal"
                    android:background="#EEEEEE" >

                    <LinearLayout
                        android:id="@+id/linearLayoutTxtView1"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:background="#EEEEEE"
                        android:layout_gravity="right" >

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:text="TextBox2"
                            android:layout_marginLeft="5px"
                            android:textColor="#000000"
                            android:gravity="center" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linearLayoutImgView"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:gravity="right|center" >

                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:layout_marginRight="16px"
                            android:text="TextBox3"
                            android:textColor="#000000"
                            android:gravity="center|right" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>
        </LinearLayout>

The problem is that I get an error when I start the app: Unable to start activity ComponentInfo{com.chiamata/com.chiamata.ChiamataActivity}: java.lang.NullPointerException This because element1 is null. If I put if(element1 != null) before adding it to main Linearlayout everything works fine, but element1 is obviously not added. I need to add it programmatically, so I can't put all in one xml file. How can I do? Thanks, Mattia

1
  • element1 is null here.do you think you need to say LinearLayout element1 = (LinearLayout) findViewById(R.id.element1); Commented Apr 19, 2012 at 11:21

3 Answers 3

12

If I correctly understand your question, you can use

LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);              

LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.Your_layout_file_with_element1 , null);

Then you can use main.addView(layout ); to add the external layout.

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

1 Comment

what is meaning of Your_layout_file_with_element1
2

you can say

LinearLayout element1 = (LinearLayout) findViewById(R.id.element1);

main.addView(element1);

1 Comment

Yes, sorry I write here wrong but in java project is called oggetto
0

Why you can't put it in one XML and then toggle visibility attribute from GONE to VISIBLE?

Comments

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.