Android | ScrollView | height wrap_content - android

How does Android think about ScrollViews?
I come from webdev background, so that is my paradigm for implementing a scrolling element.
Yet on Android, official tutorials and docs say, I should use height="wrap_content" for the ScrollView. But why?
From my understanding(webdev), ScrollView should be the container, in which it's content scrolls. Meaning ScrollView being the height restriction (therefore match_parent) and it's content should be higher than that (therefore scrolling).
If I set ScrollView height to wrap_content, and Android would actually honor that... it should not be scrollable (by my webdev paradigm) (unless Android does not add additional layout containers (outside those defined in layout file).
So how is it done?

A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout.
Here is a Link that helps you to know more about it
https://developer.android.com/reference/android/widget/ScrollView
and also here
https://www.javatpoint.com/android-scrollview-vertical

Related

Multiple children in scrollview

Is there a way to have more than one child in scrollview using a constraint layout? I'm new to coding so any help would be appreciated. I am using the latest version of android studio.
It's impossible to add more than one child into ScrollView / HorizontalScrollView / NestedScrollView
Google doc says next:
A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout.
android developers
Just wrap the children into a FrameLayout or so.
-> ScrollView
\-> FrameLayout
\-> Child 1
\-> Child 2
\-> Child 3
If you want them to be one below each other use a LinearLayout and set android:orientation="vertical".

Xamarin.Forms layout equivalent to FrameLayout

Just a beginner question about what is the equivalent to FrameLayout (Android) in Xamarin.Forms?
FrameLayout is a control that is designed to contain a single child but can optionally have multiple children that are controlled with gravity or what works similar to zindex.
I don't think there is a direct map from xamarin.Forms to Framelayout.
if your just looking for a page level container for a single control there is:
Frame-An element containing a single child, with some framing options. Frame have a default Xamarin.Forms.Layout.Padding of 20.
ScrollView-An element capable of scrolling if it's Content requires.
If your looking for a Multiple Item Container the options are:
Grid -A layout containing views arranged in rows and columns.
RelativeLayout -A Layout that uses Constraints to layout its children.
StackLayout - A Layout that positions child elements in a single line which can be oriented vertically or horizontally. This layout will set the child bounds automatically during a layout cycle. User assigned bounds will be overwritten and thus should not be set on a child element by the user.
see:https://developer.xamarin.com/guides/cross-platform/xamarin-forms/controls/layouts/
However none of these support the Z-Index like ability of FrameLayout the best option at the moment is a Grid with a single element. The children will layout from back to front in the order they are in the Grids.Children stack.

Use of Frame Layout in a project

there are many question about "FrameLayout". But i need the exact use of this different from LinearLayout, RelativeLayout. And in a project when we have to use this?
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
LinearLayout arranges elements side by side either horizontally or vertically(rows vs columns).
RelativeLayout is a layout manager that helps you arrange your UI elements based on some rule. You can specify thisngs like: align this to parents left edge, place this to the left/right of this elements etc.
Check these links
http://logc.at/2011/10/18/when-to-use-linearlayout-vs-relativelayout/
http://developer.android.com/reference/android/widget/FrameLayout.html

How to restrict bounds for Translate Animation for a view in Android?

Let me explain the scenario that I want to achieve:-
Consider the below as the Layout I have inside a Parent_Linearlayout:
[Linear Layout] (Fill_Parent, Wrap_Content)
[ScrollView]
Activity's setContentView is set to the Parent_Linearlayout
In the application, when a condition is met, I want the Scrollview to be removed from the screen and instead put another View in its place.
I've been able to do this, & when I remove the ScrollView, I'm applying translate Animation to it so that it seems as if the View has gone to the top -before removing it.
But when the animation occurs, the ScrollView translates OVER the Linear layout present above it.
How do I restrict it, so that the scrollview does not go over the linear layout, but disappears at the base of the Linearlayout. I want the linearlayout to always stay visible..
I've been trying to do this from quite some time, but I've not been able to get desired results..
Could someone kindly help me out here??
I don't quite understand your description of your layout, but the Android view system is drawn based on the ordering of the views in the hierarchy. Views added later to a parent are drawn after those added earlier. So if you always want the LinearLayout to be drawn on top of the ScrollView if/when they overlap, then declare or add the ScrollView object to its parent before the LinearLayout object.
In thinking more about this, I suppose the ordering here is important because you want the ScrollView to be placed below the LinearLayout in the parent of both of these views. Putting the ScrollView first (and thus having it painted first) would then put it above the other LinearLayout, which isn't what you want.
There are various ways to achieve what you want. For example, you could use a RelativeLayout as the parent of the views, then the ordering is not important.
Alternatively, you could place the ScrollView inside another LinearLayout (and that LinearLayout would be the second child of the overall parent layout). Then when you animate the ScrollView, it would be clipped by its immediate parent, which I believe would give you the effect you're looking for (make sure that setClipChildren() is set to true on this new intermediate LinearLayout, which it is by default, otherwise it won't clip the ScrollView as it animates out of it). Note that this approach would necessitate different animation values, since you are now animating the view outside of its parent (the new LinearLayout).

Design Custom Android ViewGroup Subclasses

Android Developer has a nice discussion on writing your own View subclasses:
http://developer.android.com/guide/topics/ui/custom-components.html
But I want to write my own ViewGroup subclass with my own child positioning policy. Where is there a minimal example of this kind of thing? (This is a Java coding question not an XML one)
Specifically I want a horizontal layout that (like LinearLayout) fills in children from the left - but once the horizontal space is consumed, shifts the children to the left so that the last child appears aligned to the right end of the layout. The children are button-like and so a HorzontalScrollView does not work since the scrolling gesture clicks the buttons instead of moving them.
If LinearLayout has an option to do this, I could not find it.
HorizontalScrollView should work, scrolling should not click the buttons. But if you really want to write your own custom layout, have a look at this archive (the video is also available)

Categories

Resources