Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add more Items in Scrollview in Android?

My android page has 10 EditText and 10 TextView. but there is no space in my screen in the Graphical Layout. i just added 5 only. im using Scroll layout. how to add additional 5 items in the screen without reducing the items height. Is there any coding here.?

like image 659
Amar Avatar asked Apr 03 '12 09:04

Amar


People also ask

How many children can a ScrollView have?

A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.

How many views can you use within a ScrollView?

Only one view can be included in a ScrollView .

What is the difference between NestedScrollView and ScrollView?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

What is the difference between RecyclerView and ScrollView?

In the practical on scrolling views, you use ScrollView to scroll a View or ViewGroup . ScrollView is easy to use, but it's not recommended for long, scrollable lists. RecyclerView is a subclass of ViewGroup and is a more resource-efficient way to display scrollable lists.


3 Answers

<ScrollView>
  <LinearLayout>
    <TextView/>..
    ....
     ..
  </LinearLayout>
</ScrollView>

You can add multiple items inside a LinearLayout. Since ScrollView is scrollable it won't affect the dimensions of the Views inside. You can add as many views as you need without worrying about screen size or View size..

like image 179
ngesh Avatar answered Sep 20 '22 23:09

ngesh


You should add a LinearLayout as the only child inside your ScrollView. Then get a reference to that LinearLayout :

mLayout=(LinearLayout)findViewById(R.id.myLayout);

and then add Views dynamically from code using :

EditText et=new EditText(...);
//....
mLayout.addView(et);
like image 26
Ovidiu Latcu Avatar answered Sep 20 '22 23:09

Ovidiu Latcu


A ScrollView can have only one direct child, so you need to put all the other Views in a Layout, such as LinearLayout and put that layout in ScrollView

Make your ScrollView's height as match_parent and the inner LinearLayout's height as wrap_content. The LinearLayout will stretch according to the number of children inside it and if the height exceeds the height of the ScrollView, the overflow can be seen by scrolling. If the ScrollView and inner Layout both have same height or if ScrollView have larger height than inner Layout, the scrolling won't happen for obvious reason.

like image 28
noob Avatar answered Sep 19 '22 23:09

noob