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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With