Android: How to preload views?
<p>I am trying to make a game where in the start menu, when a button is clicked, a story event will be happening. There will be different stories when the button is clicked depending on the character the player is using,so I don't want to make a billion different activities/fragments for the stories.</p>

<p>I have two layouts that I want to switch within the same activity.
To switch from layout 1 to layout 2, I tried to add layout 2 to layout 1, and also tried <code>ViewFlipper</code> , but when I try to switch from Layout 1 to layout 2, the <code>TextView</code> in layout 2 says "New text" first before turning into what I want it to say. The textbox, which is supposed to be at the bottom, appears at the top first and then jumps to the bottom</p>

<p>I've also tried using threads to have layout 2 preload but that just messes up layout 2. So what should I do?</p>

<p>Here is my class for the story</p>

<pre><code>public class Story extends FrameLayout {
    String[][] story;
    int H;
    int W;
    int L;
    int index =0;
    TextView text;
    ImageView character;
    View v;

Story(Context context,String[][]story,int length){
    super(context);
    this.story=story;
    this.L = length;
    init(context);
}
    public void init(Context context) {
        LayoutInflater l = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = l.inflate(R.layout.story, this, true);
        final RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final FrameLayout.LayoutParams imageParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        text = (TextView) v.findViewById(R.id.story);
        character = (ImageView) v.findViewById(R.id.chara);
        final RelativeLayout textBox = (RelativeLayout) v.findViewById(R.id.textbox);
        textBox.post(new Runnable() {
            @Override
            public void run() {
                H = textBox.getHeight();
                W = textBox.getWidth();
                textParams.setMargins(W / 12, H / 8, W / 10, H / 8);
                text.setLayoutParams(textParams);
                text.setText(story[index][0]);
            }
        });
        v.post(new Runnable() {
            @Override
            public void run() {
                int h = v.getHeight();
                imageParams.setMargins(W / 7 * 2, h / 10, 0, H / 3);
                character.setLayoutParams(imageParams);
                character.setImageResource(Integer.parseInt(story[index][1]));
            }
        });
    }
}
</code></pre>

<p>XML for the story</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;merge xmlns:android="http://schemas.android.com/apk/res/android"
     &gt;

    &lt;ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chara"
        android:scaleType="fitStart"
        android:adjustViewBounds="true"
        /&gt;
    &lt;RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textbox"

        android:layout_gravity="bottom"&gt;
        &lt;ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/vntextbox"
            android:adjustViewBounds="true"
            android:scaleType="fitEnd"&gt;

        &lt;/ImageView&gt;

        &lt;TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/story" /&gt;
    &lt;/RelativeLayout&gt;

&lt;/merge&gt;
</code></pre>

<p>For each character, they have a method that returns their story, eg for a particular character : </p>

<pre><code>public class Player extends ImageView {  
    Context context;
    Player (Context context){
        super(context);
        this.context=context;
    }
public Story getIntro(){
    int normal = R.drawable.normal;
    int serious = R.drawable.serious;
    int happy = R.drawable.happy;
    final int length = 8;
    String[][] story = // a story
    final Story s = new Story(context, story, l);
            s.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                 //do some stuff
                }
    });
    return s;
}
}
</code></pre>

<p>And here is where the story is used</p>

<pre><code>public class StartMenu extends AppCompatActivity {
   @Override
    protected void onCreate(final Bundle savedInstanceState){
       super.onCreate(savedInstanceState);

setContentView(R.layout.startmenu);
 final RelativeLayout r = (RelativeLayout)findViewById(R.id.startscreen);
       final Button b =new Button(this);
b.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             //here I want to do something like this: (doesn't work due to problems described above)
                             Player p = new Player(StartMenu.this);
                             Story s = p.getIntro();
                             r.removeAllViews();
                             r.addView(s);
}
});
</code></pre>