2

I want to add horizontal and vertical scroll view in my Android application at runtime.

ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.drawable.opsbuds);
ll.setLayoutParams (new LayoutParams 
        (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(40, 20, 0, 0);
sv.addView(ll);

now my code is like this, but it is not working properly

ScrollView sv = new ScrollView(this);
HorizontalScrollView horizontal = new HorizontalScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.drawable.opsbuds);
ll.setLayoutParams (new LayoutParams 
        (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(40, 20, 0, 0);
horizontal.addView(ll);

by using above code I can add vertical scroll view. but when I use both horizontal ans vertical it is not coming properly...

JJD
  • 50,076
  • 60
  • 203
  • 339
Cool Compiler
  • 857
  • 2
  • 9
  • 20
  • so far i am able to add veritcal scrollview at runtime but i want both horizontal and vertical – Cool Compiler Jun 20 '12 at 06:23
  • You can implement a `HorizontalScrollView` in the same way as a `ScrollView`. What part of this are you struggling with? Do you want both vertical *and* horizontal scrolling for the same `View`? – Cat Jun 20 '12 at 06:24
  • yeah.. i should be able to scroll vertically and horizontally also. – Cool Compiler Jun 20 '12 at 06:27
  • possible duplicate of [Scrollview vertical and horizontal in android](http://stackoverflow.com/questions/2044775/scrollview-vertical-and-horizontal-in-android) – Squonk Jun 20 '12 at 06:52

1 Answers1

0

You need a layout in your ScrollView. It doesn't especially matter what that layout is, but I like to use RelativeLayouts. Your ScrollView should have only a layout child, and that layout should have a HorizontalScrollView child.

samdoj
  • 144
  • 8