Adding a conditional number of elements to a Scroll View in Android Studio

209 views Asked by At

Suppose within a scroll view I want to have n rows, where n is a number determined by how many items were selected using check boxes on a different screen. I restricted the maximum height the scroll view can take up on the screen by putting it inside a linear layout. Normally you could just add elements by doing something like the following:

<LinearLayout>
   <Scroll View>
        <TextView>
        <TextView>
         ...
    </ScrollView>
</LinearLayout>

but since the number of TextView objects is dependent on how many check boxes were selected on the previous screen, how could I make sure that I add the appropriate number of textView objects within the scrollView?

1

There are 1 answers

0
Abu Yousuf On

Best way is to use RecyclerView. If you still want to use ScrollView then create a layout for each row or each item. Add that layout to ScrollView from your Java class programmatically number of times you want.

LinearLayout scrollLayout = ......
scrollLayout.removeAllViews();
for(int i = 0; i<totoalSelected; i++){
   View view = LayoutInflater.from(context).nflate(R.layout.my_layout,null,false);
   // initialize view with data if needed
   scrollLayout.addView(view);
}