Here is the code that work :
In the code below everything work, but when i try to add a second view it crashes.
public void ClearAllV() {
ImageView IM2 = new ImageView(this);
HorizontalScrollView SW = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
HorizontalScrollView.LayoutParams lp = new HorizontalScrollView.LayoutParams(
HorizontalScrollView.LayoutParams.WRAP_CONTENT,
HorizontalScrollView.LayoutParams.WRAP_CONTENT);
SW.removeAllViews();
IM2.setImageResource(R.drawable.have_fun);
SW.addView(IM2, lp);
}
But if i try to add a second imageview like the one below it crash,
public void ClearAllV() {
ImageView IM2 = new ImageView(this);
ImageView IM3 = new ImageView(this);
HorizontalScrollView SW = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
HorizontalScrollView.LayoutParams lp = new HorizontalScrollView.LayoutParams(
HorizontalScrollView.LayoutParams.WRAP_CONTENT,
HorizontalScrollView.LayoutParams.WRAP_CONTENT);
SW.removeAllViews();
IM3.setImageResource(R.drawable.have_fun);
IM2.setImageResource(R.drawable.have_fun);
SW.addView(IM2, lp);
SW.addView(IM3, lp);
}
The
ScrollViewandHorizontalScrollViewshould only hold one child.Usually you would have a
LinearLayoutas the only child of theHorizontalScrollView, and add your views to that.From the ScrollView-documentation:
Note that this is about the ScrollView (not the HorizontalScrollView (the hsv doc says the same thing but with horizontal instead of vertical), and thus mentions a vertical layout.