Android: Inflate View under a View then slide top view off
<p>My issue is that I have a main screen, and I would like to dynamically spawn a view under it with a button click, then slide the main view off the screen revealing the view below it. I've accomplished this, but I feel like there's got to be a better way. The way I've done it is very limited in that you can't just spawn views over and over again under the main.</p>

<p>My main XML file looks like this:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
&gt;
  &lt;RelativeLayout
    android:id="@+id/subpage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;
  &lt;/RelativeLayout&gt;
  &lt;RelativeLayout
    android:id="@+id/homescreen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;
    &lt;RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/homebg"
        &gt;
    &lt;/RelativeLayout&gt;
  &lt;/RelativeLayout&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p>I've deleted some unnecessary stuff. This is what's important. Notice the first child of the main layout is a relative layout with the id "subpage." As it is I use java to inflate another layout into the subpage layout when a button is clicked then I animate the "homescreen" layout off the screen. It seems like I shouldn't have to have the subpage declared in advance though. I guess my question is, is there a way to dynamically declare a new child layout underneath an existing layout?</p>

<p>=======================================================================</p>

<p>Edit: Part 2 of question</p>

<p>I'm trying to use addView and the app crashes. This is the code I use to try to add a view and inflate my xml into it. In the code below subview is a ViewGroup because as I understand it you can only inflate into ViewGroups, not regular views. Also 'activity' is defined at the top of the class as 'private Activity activity = this'. Any ideas what could be causing the crash?</p>

<pre><code>btnHelp.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            subView = (ViewGroup)new View(getApplicationContext());
            mainScreen.addView(subView,1);
            LayoutInflater inflater = activity.getLayoutInflater();
            inflater.inflate(R.layout.help, subView);
        }
    });
</code></pre>

<p>=======================================================================</p>

<p>Edit: Part 3 of question</p>

<p>So one more issue. Everything works great as far as inflating and sliding off. However, the view that is inflated has a button in it. I'm trying to assign a listener to that button, but it doesn't seem to work. I'm doing it by adding the listener to the button after the layout inflater is called in the btnHelp I've been working on. Here's the code:</p>

<pre><code>btnHelp.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            LayoutInflater inflater = activity.getLayoutInflater();
            mainScreen.addView(inflater.inflate(R.layout.help, null),0);
            homeScrn.startAnimation(slideLeftOut);
            btnBackHome = (ImageView)findViewById(R.id.backMenuBtn);
            btnBackHome.setOnClickListener(goHome);

        }
    });
</code></pre>

<p>goHome is a handler I've defined below this as such:</p>

<pre><code>private OnClickListener goHome = new OnClickListener(){
    public void onClick(View v) {
        Log.d("ClickEvent: ","btnBackHome Clicked");
        homeScrn.startAnimation(slideRightIn);
    }
};
</code></pre>

<p>When I click the button referenced by btnBackHome it doesn't do anything. I'm just not sure if it's because the listener isn't actually being assigned, something is keeping the button from actually being clicked, or something else.</p>
