Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add views to a scrollview without inflating/re-inflating layouts?

I would like to take an existing ScrollView with a view in it, and add more views, dynamically (at runtime) to the ScrollView container.

Is it possible to add these views without having to create a new layout and inflate it? If so, what's the general process for adding these views dynamically?

For the sake of this question, assume the views are TextView...

Thanks!

like image 852
Brad Hein Avatar asked Jan 13 '11 17:01

Brad Hein


People also ask

What is the difference between ScrollView and horizontal ScrollView?

ScrollView and HorizontalScrollView has same attributes, the only difference is scrollView scroll the child items in vertical direction while horizontal scroll view scroll the child items in horizontal direction.

Why do we use the layout Inflater in ListView?

Put simply, an inflater allows you to create a View from a resource layout file so that you do not need to create everything programmatically.


1 Answers

A ScrollView can only have one child, so it doesn't make sense to add more children to it directly. Lets say your ScrollView has a LinearLayout inside of it, then you can add more views to the LinearLayout:

LinearLayout layout = findViewById(R.id.my_linear_layout);
TextView textView = new TextView(this);
layout.addView(textView);
like image 199
Cheryl Simon Avatar answered Sep 20 '22 22:09

Cheryl Simon