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?

4
  • What are you expecting to happen? You haven't added any views to item_box, so it's not going to be visible. Commented Jan 21, 2012 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. Commented Jan 21, 2012 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? Commented Jan 21, 2012 at 18:29
  • possible duplicate of How to inflate one view with an layout Commented Jan 21, 2012 at 18:45

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Best answer.Gold slotion.Like ;)
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);

Comments

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.

Comments

1

Use LayoutInflater:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);

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.