Reputation: 4401
I want to add images to scrollview, there is code i tried to use:
ScrollView sv = (ScrollView)findViewById( R.id.scrollView2);
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "PATH" ) );
iv.setScaleType( ScaleType.CENTER_INSIDE );
sv.addView( sv );
I get this exception: java.lang.IllegalStateException: ScrollView can host only one direct child
So how i should add images to my scrollview ?
Added code: xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FF0000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</ScrollView>
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/scrollView1"
android:background="#FFFF00" >
<LinearLayout
android:id="@+id/filesScrollerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</ScrollView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#FFFFFF"
android:layout_toRightOf="@+id/scrollView2" />
</RelativeLayout>
And on the end Activity onCreate calling this method:
public void addImage(String path){
LinearLayout sv = (LinearLayout)findViewById( R.id.filesScrollerLayout);
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( path ) );
iv.setScaleType( ScaleType.CENTER_INSIDE );
sv.addView( sv );
}
Thanks.
Upvotes: 1
Views: 18135
Reputation: 13501
this may help.. it tells so because ScrollView cannot hold more than 1 child.. it needs single child which hosts all other view..
<ScrollView>
<LinearLayout
android:id="@+id/child">
<ImageView/>
...
...
</LinearLayout>
</ScrollView>
in your case
LinearLayout child = (LinearLayout)findViewById( R.id.child);
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "PATH" ) );
iv.setScaleType( ScaleType.CENTER_INSIDE );
child.addView( sv );
Upvotes: 9