1

I have a LinearLayout in main.xml:

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

</LinearLayout>

I have made another XML file, called item_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/item_bg"
    android:gravity="right" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="20dp"
        android:text="@string/item1"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="20dp"
        android:gravity="right"
        android:text="@string/number1"
        android:textColor="@color/number_bg"
        android:textSize="30dp" />

</LinearLayout>

Basically, what I want to do from the code (programmatically), is add a couple item_box.xml into the main.xml. How can I accomplish this?

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Qasim
  • 1,686
  • 4
  • 27
  • 51
  • What are you expecting to happen? You haven't added any views to `item_box`, so it's not going to be visible. – Brigham Jan 21 '12 at 18:26
  • But I keep getting a Nullpointer exception, and the view doesnt show on my device when I play it. I have made an include called "item_box.xml", and I want to add it to the main layout. – Qasim Jan 21 '12 at 18:27
  • What do you mean by "isn't working"? Another thing - it seems that both LLs are defined in XML, why do you want to rearrange them from code? Could you post your layout file too? – molnarm Jan 21 '12 at 18:29
  • possible duplicate of [How to inflate one view with an layout](http://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-an-layout) – Qasim Jan 21 '12 at 18:45

4 Answers4

5
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
mainLayout.addView(itemBox);
user634545
  • 9,099
  • 5
  • 29
  • 40
2

try this linearlayout inside another linearlayout add id to linearlayout layout1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/firstlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

in oncreate() call

LinearLayout firstlayout=(LinearLayout)findviewbyId(R.id.firstlayout);
LinearLayout secondlayoout=(LinearLayout)this.getLayoutInflater().inflate(R.layout.layout2,null); //inflating view from xml 
TextView btn1=(TextView)secondlayoout.findviewbyId(R.id.button1);
btn1.settext("TEST");
firstlayout.addview(secondlayoout);
Satheeshkumar Somu
  • 496
  • 1
  • 7
  • 14
1

You are getting a Nullpointer because linearLayout1 is not in mainLayout. You need to inflate your view first and then add it. You should read this question I think it will help you.

Community
  • 1
  • 1
theJosh
  • 2,894
  • 1
  • 28
  • 50
1

Use LayoutInflater:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
Brigham
  • 14,395
  • 3
  • 38
  • 48