0

Currently trying to add a layout (box.xml) to my main layout (activity_main.xml) from a different activity, but having trouble correctly doing this. Here is an example of what I'm trying to do:

(Note: removed params for simplicity)

activity_main.xml

<FrameLayout>
    <ScrollView>
        <LinearLayout>

            <!--include layout="@layout/box"-->

        </LinearLayout>
    </ScrollView>
</FrameLayout>

box.xml

<TableLayout>
    <TableRow>
        <TextView/>
        <ImageView/>
    </TableRow>
    <TableRow>
        <TextView/>
        <Button/>
    </TableRow>
</TableLayout>

CreateNewBoxActivity.java

(Note: My MainActivity.java calls startActivity(new Intent(this, CreateNewBoxActivity.class)))

public class CreateNewBoxActivity extends Activity {

   private ImageButton  mFinish;
   ...

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_newBox);

      mFinish = (ImageButton) findViewById(R.id.btn_finish);

      mFinish.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View view)
         {

            //add box.xml where <include layout> is located in activity_main.xml

            finish(); //close this activity and back to main activity to see 
                      //added box layout
         }
      });
      ...

   }
}

Thanks for any help on how to add this layout! Not sure the best way to tackle this.

Adam
  • 241
  • 1
  • 3
  • 11
  • Do you **have** to do it programmatically or can you add it to your xml? – codeMagic Feb 13 '14 at 18:52
  • Why not add it in the xml and toggle visibility? – FD_ Feb 13 '14 at 18:56
  • @codeMagic: I would like to add the boxes programmatically to make adding the boxes dynamic for better user control. I don't want the boxes there at all unless the user wants to create a new one. – Adam Feb 13 '14 at 18:59
  • @FD: I was considering using ViewStub to maybe set a limit to how many boxes a user can add, but I was having trouble finding main_activity.xml ViewStub from my CreateNewBoxActivity since my setContentView is set to R.layout.activity_newBox – Adam Feb 13 '14 at 19:02

1 Answers1

0

You will want to use a LayoutInflater to inflate box.xml. Make sure you have an id for the LinearLayout that you want to add the box.xml to. Inflate that layout with something like

LinearLayout ll = (LinearLayout) findViewById(R.id.llId);

then you can use addView to add the box layout to your LinearLayout.

See this answer for a more detailed example

Edit

After further discussion, the OP wants to add certain elements from the layout in the second Activity to the layout used in the first Activity. This would be accomplished easier by using startActivityForResult() and passing back the data to the first Activity.

Then, in onActivityResult() in the first Activity the appropriate layout can be added to the original layout using the previously mentioned approach (inflating and calling addView()).

How to handle the data and what needs to be added depends completely on how this is handled by the dev. Data could be added to a model class from the second Activity then checked and added in onActivityResult() or this same method could check for Intent data passed back through setResult().

Intent Docs contains an example and the method needed to pass data back.

This answer has a brief example of using startActivityForResult() and passing back Intent data.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • When I inflate my box.xml, what do I pass in for the ViewGroup parameter? TableLayout box = (TableLayout) inflater.inflate(R.layout.box, ? ); – Adam Feb 13 '14 at 19:22
  • Either `null` or the `ViewGroup` to attach it to. I think [this answer](http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater) explains it fairly well. – codeMagic Feb 13 '14 at 19:26
  • I'm getting a nullpointerException on my LinearLayout, is this because that layout is located in my activity_main.xml? I was able to inflate my box.xml (by using debugger) but can't seem to add that view to my linearlayout – Adam Feb 13 '14 at 19:48
  • Oh yes, sorry. I thought you were trying to do it in the `Activity` that had `main` as its `layout`. You want to put it in a `layout` that isn't inflated for that `Activity`??? That's backwards and won't work. – codeMagic Feb 13 '14 at 19:50
  • Sorry if I made this confusing :( Yea I basically wanted to add the box.xml to the LinearLayout in activity_main.xml while in the activity CreateNewBoxActivity that had a different layout for its setContentView() call. – Adam Feb 13 '14 at 20:00
  • No, that's backwards. Make `activity_main` the `layout` with `setContentView()` then allow the user to add the `box` layout to it. – codeMagic Feb 13 '14 at 20:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47458/discussion-between-a-donahue-and-codemagic) – Adam Feb 13 '14 at 20:19