I have a ScrollView and I want to add 2 images on it.
I created a ScrollView and then a LinearLayout. Then created 2 ImageViews and added them on LinearLayout. Then added LinearLayout on ScrollView. (All of steps by java code. Didn't use XML at all)
But the problem is the ScrollView height is not fitted to height of my images(It's too long).
This is my code:
LinearLayout scrollParent = new LinearLayout(this);
scrollParent.setOrientation(LinearLayout.VERTICAL);
scrollParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
helpView = new ScrollView(this);
helpView.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
helpView.setFillViewport(true);
inside = new LinearLayout(this);
inside.setOrientation(LinearLayout.VERTICAL);
inside.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
helpView.addView(inside);
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.learn1);
img.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ImageView img2 = new ImageView(this);
img2.setImageResource(R.drawable.learn2);
img2.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
inside.addView(img);
inside.addView(img2);
scrollParent.addView(helpView);
Thanks for your help.
Try this...
activity_demo.xml
Demo.java