Search code examples
androidlayoutscrollviewandroid-image

Adding list of images into a scroll view


I want to have a scroll bar in android showing each individual plant such that a user can press on one to select it, create a new Plant object he can then move around the screen.

I'm running into trouble populating this list.

I have a HorizontalScrollView:

bar = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);

The brunt of my code happens in

private LinearLayout loadBar(ArrayList<Plant> listOfPlants, HorizontalScrollView bar){
     LinearLayout layout = new LinearLayout(getApplicationContext());

     layout.setHorizontalScrollBarEnabled(true);
     ImageView image= new ImageView(getApplicationContext());
     ArrayList<View> images = new ArrayList<View>();
     for (int i = 0; i < list.size(); i++) {

         image = listOfPlants.get(i).getPhoto();
         images.add(image);

    }
    layout.addTouchables(images);
    return layout;
 }

I want to add this linear layout to the scroll bar I'm creating but I don't see any way to do this. Or am I going about this the wrong way and is there a much simpler way to get what I want done?


Solution

  • You can take a ListView inside LinearLayout, that is inside horizontal scroll view as below:

    <HorizontalScrollView ...........>
    <LinearLayout ......>
        <LinearLayout ......>
        //List View Titles will be here
        </LinearLayout>
    
        <ListView ........ android:layout_weight="1" />
    
    </LinearLayout>
    </HorizontalScrollView>
    

    Also, it is important to put layout_weight in the ListView. Hope it works for you. Also, you can check this customized horizontally scrollable listview tutorial.