title: Android how to programmatically create scrollview and add programmatically created views into it
body: <p>Alright I'm trying to build an activity that has a horizontal scrollview, that the user can swipe through, to view different "pages".  My train of thought is these "pages" will be views.  The following is a mockup of my idea (to mess around to see if it works)</p>
  
  <p>I've experimented with this as follows:</p>
  
  <p>My content view is set to the the scrollview. (unsure if this is an incorrect approach)</p>
  
  <p>I create the scrollview, and place a view into it as follows:</p>
  
  <pre><code>private void setupScrollView()
  {
      Display display = getWindowManager().getDefaultDisplay();
      DisplayMetrics outMetrics = new DisplayMetrics ();
      display.getMetrics(outMetrics);
  
      float density  = getResources().getDisplayMetrics().density;
      float dpHeight = outMetrics.heightPixels / density;
      float dpWidth  = outMetrics.widthPixels / density;
  
      int width = (int)MeasureUtils.convertDpToPixel(dpWidth, getApplicationContext());
      int height = (int)MeasureUtils.convertDpToPixel(dpHeight, getApplicationContext());
  
      _scrollView = new HorizontalScrollView(getApplicationContext());
      _scrollView.setBackgroundColor(Color.CYAN);
      _scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  
       Log.i("DEBUG", "Screen dp width = " + dpWidth + " screen dp height = " + dpHeight);
  
      TextView view = new TextView(getApplicationContext());
      view.setBackgroundColor(Color.RED);
      view.setText("TEST");
  
      view.setX(0); // Start at the left of the scrollview.
  
      view.setWidth(width); // Size it so that it fills to the right of the scrollview.
  
      TextView view2 = new TextView(getApplicationContext());
      view2.setBackgroundColor(Color.GREEN);
      view2.setText("TEST2");
  
      view2.setX(width); // Start the second "page/view" offscreen to the right where i can scroll to it
  
      view.setWidth(width); // Fill the screen width
  
      LinearLayout layout = new LinearLayout(getApplicationContext());
      layout.setBackgroundColor(Color.MAGENTA);
  
      layout.addView(view);
  
      layout.addView(view2);
  
      _scrollView.addView(layout);
  }
  </code></pre>
  
  <p>The idea above is that I will see a view, that takes up the screen, representing a page.  This view should be "RED" in color.  I can then scroll horizontally to the right and see the second view (view2) representing the next page.  This view should be "GREEN" in color.  This does not happen.  I end up seeing what looks like 1/3rd or 1/2 of my screen being view1, the linearlayout taking up <em>almost</em> the whole screen (a bit of a gap to the right edge where the CYAN from the scrollview bleeds through). </p>
  
  <p>Am I approaching this the wrong way, and/or is it possible to make this work the way I'm going at it?</p>