I have a LinearLayout inside a ScrollView. At some point I collapse and expand the LinearLayout. After that the scrollview does scroll. Any ideas?
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="false">
<LinearLayout
android:id="#+id/detailsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible"
>
........
</LinearLayout>
</ScrollView>
Expand and collapse is done using ValueAnimator over LayoutParams (height) of the LinearLayout.
Update: I think important is that it breaks down after animated expansion/collapse. Until that it works fine.
Update 2: For expanding again, I measured the expected height as follows:
int expectedHeight = detailsView.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
and animate expansion to that height. This measured the height to 2000+ instead of expected 800 something. Thus the sizes were equal and scrollview didn't scroll, although it didn't show the full hierarchy.
As a quick-fix I save the current height before collapsing and use it as the target height on expanding. The question is, can this be done automatically without dirty-height saving?
your scrollview is android:layout_height="wrap_content" just make it match_parent. I'm not sure but you can remove android:fillViewport too
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/detailsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible"
>
........
</LinearLayout>
</ScrollView>
Related
I have this code. So I want to set height in my relativeLayout by height in scrollView. Can I set height by id of scrollView? It's need, because I have many fragments, but one of them should take height of scrollView (when virtual keyboard is open).
...
<ScrollView
android:id="#+id/scrollViewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:id="#+id/frameViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/layoutViewer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webViewer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</FrameLayout>
</ScrollView>
...
ScrollViews child (and its childrens also) should have set layout_height="wrap_content" for avoiding multiple re-measurements (way better performance)
if you want to fullfil whole ScrollView even when content have less height then use fillViewport xml attribute
So I have a layout with a ScrollView. When I test my code, the ViewPager-heigth jumps from a reasonable value (1845) to very small (so fast the eye can't see). If I switch the layout_height of the ViewPager to wrap_content the height jumps to 0.
Aditionally, when the ViewPager has a heigth > 0, the top half of it is outside the Screen and only the bottom half is visible.
Clearly I'm doing something wrong here:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/colorDeadBackground"
android:fillViewport="true">
<LinearLayout
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:outlineProvider="bounds"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:elevation="2dp"
app:cardCornerRadius="6dp">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager_pics"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.finder.ViewPagerIndicator.LinePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="4dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
app:strokeWidth="2dp"
app:unselectedColor="#88888888"
app:selectedColor="#color/colorWhite"/>
</androidx.cardview.widget.CardView>
...
If I set the height to a fixed value (either programatically or in the xml) the problem persists, that only the top half of the ViewPager is seen. Even the preview Window says its only half on the screen:
you are setting the CardView height and ViewPager height both on match_parent, which will cause an issue in visibitily , try to set a fixed size to height for both of them !
To get rid of the weird offset thing (that the ViewPager starts obove the screen), I had to remove the android:layout_gravity="center" from the Linearlayout. No idea why, if someone explains that to me, that would be dope. Probably has soemthing to do, that I used android:layout_gravity="center" twice in the Linearlayout and in the CardView.
Then I still ahd to set the height of the ViewPager programatically, so that the height isn't 0. No idea why either, doesn't matter if I changed match_parent to wrap_content anywhere.
In the following code I've got two main containers, a RelativeLayout and a ScrollView. I need the RelativeLayout to be on top fixed and below that I need the ScrollView with Images to be scrollable. I've got the following two questions:
Though my ScrollView is scrollable but it goes on top of the RelativeLayout. !important
The view of my images present within the vertical LinearLayout are thumbnail sized. If I have 15-20 images, they take a lot of vertical space. How do I have the images to fit the maximum in a row based on the screen size and then go to the next row?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView>
</ImageView>
<ImageView>
</ImageView>
<ImageView>
</ImageView>
<ImageView>
</ImageView>
</LinearLayout>
</ScrollView>
Follow these in simple way:
You need to use a fixed height for the parent, i.e., <RelativeLayout>.
Set the height of the ScrollView to match_parent.
Make sure you put everything inside a LinearLayout.
Finally you would end up with this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</RelativeLayout>
</LinearLayout>
First, you can set your RelativeLayout's height to a fixed value. Then set your ScrollView's height to match_parent, so it takes up all the available space. Then both of these should be contained in a vertical LinearLayout, to avoid any overlapping.
I have the following:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layout_question_types"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18sp"
android:layout_marginTop="18sp"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- some content -->
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="18sp"
android:gravity="right"
android:orientation="vertical" >
<!-- some more content -->
</RelativeLayout>
</LinearLayout>
</ScrollView>
However, when I turn the phone horizontal to force the scrollbar, the scrollbar appears but the content at the bottom of the screen gets hidden.
Any ideas? There are similar questions on SO but they don't seem to fix the problem. Thanks.
Reason of this are margins and ScrollView parent FrameLayout which has some margin problems. Margins are ignored in measuring and scroll view measure its size without these margins and that is why the bottom part of inner views in ScrollView is not visible.
You can solve it simple wrapping child LinearLayout with another LinearLayout without margins.
Is it possible to use long lists of widgets inside RelativeLayout, which in turn is wrapped into ScrollView.
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="800dp"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="800dp"
>
<TextView
android:id="#+id/screen_size_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:text="#string/screen_size" />
<TextView
android:id="#+id/screen_size_label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/screen_size_label"
android:layout_below="#+id/screen_size_label"
android:text="#string/screen_size_label" />
<TextView
android:id="#+id/screen_size_label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/screen_size_label"
android:layout_below="#+id/screen_size_label2"
android:text="#string/screen_size_label" />
// each following child uses android:layout_below="#+id/previous"
When I paste considerable amount of widgets so that the bottom of the screen is achieved the next don't get placed under the previous ones, as I expected, but instead they try to fit in the screen-box resulting in mess. What I need instead is to place them one under another - so that those that don't fit into screen-box become accessible through scrolling.
It works fine when I use LinearLayout instead of RellativeLayout, however I'd like to get use of RelativeLayout if it's possible.
Thanks.
Change the height of Scrollview and RelativeLayout to "fill_parent"
You define scrollview's height and relativelayout's height to same, then its' no need to scroll. So you change scrollview's height to fill_parent, and relativelayout's height to wrap_content.
What if you change the height of the Scrollview and RelativeLayout ..
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>