Search code examples
androidandroid-scrollview

Android: How do I create a small ScrollView?


I would like to create a scrollview that occupies a smaller area of my screen. My device has a resolution of 1280x768. I know I should be using weights etc but I'm programmatically trying to solve another problem and get my head around how to specify the scrollview "viewable area window size" vs the virtual area size that scrolls behind it.

I create a ScrollView like this:

ScrollView testScrollView     = new ScrollView( this );
testScrollView.setBackgroundColor( Color.MAGENTA );
testScrollView.setLayoutParams( new LinearLayout.LayoutParams( 200, screenHeight/2 ) );
//testScrollView.addView( layout );

setContentView( testScrollView );

Now why is it taking up my entire screen when I tell is explictly to be 200 pixels wide?


Solution

  • (I guess)that's because you are setContentView to the ScrollView. (Suppose that your activity is (nearly) full screen but your content view is only 200pix wide, what the other part will be on the screen?).

    You can try adding a FrameLayout that wrapps the small ScrollView and set the frame layout as content view.