I have a ScrollView that contains a LinearLayout.
In code I add items to the LinearLayout and it nicely expands.
When I remove, in code, all the items from the LinearLayout, the LinearLayout stays the same size, it's empty though.
How do I get the ScrollView to reclaim the height that the LinearLayout used when it had items and shrink back the LinearLayout to use the least height as it is now empty?
I have tried (in C#):
linearLayout.RemoveAllViewsInLayout();
linearLayout.PostInvalidate();
scrollView.PostInvalidate();
Java or C# answers are fine.
I only have Java experience with Android, so I would try using:
linearLayout.requestLayout();
This should cause another layout pass, and assuming your LinearLayout is set to WRAP_CONTENT it will measure all the children and size itself to fit the smallest space that the children occupy (or minHeight if you have that set and it is larger).
This trick seems to work. The result is that the linearlayout ids resized and the scrollview as well:
linearLayout.RemoveAllViewsInLayout();
linearLayout.Visibility = ViewStates.Gone;
scrollView.SmoothScrollTo(0, 0);
linearLayout.Visibility = ViewStates.Visible;
There must be a better way, but this works.
Related
I have a ScrollView which child's height is 500px,this contain 2
button and the sum of height of them is 500px.but the value of
child.getHeight() is 788 not 500 I know ScrollView extends Framelayout,i replace the ScrollView with Framelayout in the same scene and the value of child.getHeight() is 500.
Why is this happen?
As per my knowledge and what i found on serch is, when you use scroll view the scroll view adds some padding inside its child elements for its comfort. but in case framelayout this not happens so you are getting these different values for scrollview and framelayout. as you can see android:margin not works for scrollview but works for framelayout. you can see these two examples for understanding-
margin issue
difference between padding and margin
First, I want to list tags in a whatever layout. The container's max width is fixed (match_parent), and the tags (TextViews) should no exceed the right border of the container. Meanwhile, the container is a direct child (LinearLayout in my case) of a ScrollView (I emphasize this as I find some solutions using customized onMeasure and onLayout methods extending ViewGroup, but cannot work in ScrollView).
How can I do this?
This could be solved with FlowLayout things, as commented by WaJeEh.
What's the best approach to have a layout with the following:
Spinner (default height)
WebView (all the space between the two views (Spinner and Button))
Button (default height)
How is it possible to specify the height of the WebView to take all the space between the two (i.e. if they are resized later, the WebView will automatically be adjusted).
Thanks!
Use a RelativeLayout as the parent layout. Align the Spinner to the top of the parent. Align the Button to the bottom of the parent. For the WebView, set the height as match_parent and then use the android:layout_below and android:layout_above attributes to make sure that it always lies between the two views.
(I would've given the code but I don't want to. You might just copy paste with zero learning.)
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.?
<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..
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);
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.
I'm adding a TextView to a parent LinearLayout RelativeLayout programmtically. There is enough space for the text to be displayed horizontally but for some reason the text is displayed vertically. Does anyone know what's going on here?
It's really hard to say what is the problem without looking at your XML and layout code. In my experience, sometimes it happens when the parent layout has height set as WRAP_CONTENT and the views inside it have wrong weight config, for example one has weight but WRAP_CONTENT and the other MATCH_PARENT (Sorry, I don't remember the case exactly). I suggest you check your LayoutParams carefully or set a fixed width and height for the TextView to see what is the problem.
You can also post your code here so we can have a look at it