title: The best way to inflate view programmatically
body: <p>I found a simple SwipeSample that I changed to allow me to create new xml layouts and inflate the main layout to display them. What I wanted to do was also be able to programmatically add layouts for the swipe process.</p>
  
  <p>I have the main.xml layout and a red.xml and yellow.xml which are a simple linearlayout with a textview set to a solid color.</p>
  
  <p>The code below works but I don't think that it's correct or the best way to do what I'm trying to get. 
  If anyone can suggest a better way that would be greatly appreciated.</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      //Create a layout with a solid blue background programmatically
      TextView tv1 = new TextView(this);
      tv1.setText("Blue");
      tv1.setBackgroundColor(Color.BLUE);
      tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  
      LinearLayout ll = new LinearLayout(this);
      ll.setOrientation(LinearLayout.VERTICAL);
      ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
      ll.addView(tv1);
      //Create a layout with a solid green background programmatically
      TextView tv2 = new TextView(this);
      tv2.setText("Green");
      tv2.setBackgroundColor(Color.GREEN);
      tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  
      LinearLayout ll2 = new LinearLayout(this);
      ll2.setOrientation(LinearLayout.VERTICAL);
      ll2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
      ll2.addView(tv2);
      //inflate the flipper view and add the yellow and red xml layouts and also the 2 programmatically created layouts
      fSpace = (ViewFlipper)findViewById(R.id.flipper);
      inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      inflater.inflate(R.layout.yellow, fSpace);
      inflater.inflate(R.layout.red, fSpace);
      fSpace.addView(ll);
      fSpace.addView(ll2);  
  
  }
  </code></pre>