How to remove extra space from the bottom of Scrollview. Below is the XML layout where I am including two external layouts. Have refered many solutions but none of them worked for me.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sc"
android:layout_marginBottom="55dp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay"
android:layout_weight="1"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/linprodetaill"
android:layout_above="#+id/bt"
android:layout_height="wrap_content">
<include
android:id="#+id/l1"
layout="#layout/productexpand_items" />
<include
android:id="#+id/l3"
layout="#layout/dynamic_view"
/>
</LinearLayout>
</ScrollView>
Remove android:layout_marginBottom="55dp" from your ScrollView
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sc"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay"
android:layout_weight="1"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/linprodetaill"
android:layout_above="#+id/bt"
android:layout_height="wrap_content">
<include
android:id="#+id/l1"
layout="#layout/productexpand_items" />
<include
android:id="#+id/l3"
layout="#layout/dynamic_view"
/>
</LinearLayout>
</ScrollView>
Remove this line from your xml
android:layout_marginBottom="55dp"
it will remove the extra space , u can set 10dp just to give enough spacing
nest your scrollView and the other layouts in a Relative layout.
and then use the layout_below attribute to align the layouts below your scrollView.
and make sure to inclue layout_alignParentTop="true" in your scrollView's xml and remove the bottom margin of your scrollView.
now when the bottom layouts have their visibility value changed to Visible or Gone, scrollView will fill the parent or leave room for them,
Related
I am putting the cardview inside scrollview, we expect to see that at the bottom, the border should be shown(see pic below). But its not. The problem is that I cannot scroll to the bottom to see the border of cardview.
All the solutions on SO is to change layout_margins to paddings, but its not the case for cardview if we want to show the border. I basically tried everything. But still doesnt work.
Picture 1. scroll to bottom cannot see the border
Picture 2. We can see the top border
Following is xml code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
...
</LinearLayout>
</CardView>
</LinearLayout>
references:
ScrollView doesn't scroll to the bottom
ScrollView cuts off the top and leaves space at the bottom
I can't show LinearLayout at bottom to scroll view
Android ScrollView refuses to scroll to bottom
UPDATED
This will work better now with a NestedScrollView and a MaterialCardView. I added this NestedScrollView to a ConstraintLayout.
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true">
This is working for me without the LinearLayout wrapper.
===========================================================================
OLD WAY LEFT HERE
I ran into the same problem and had to do the following (the key is the LinearLayout wrapper around the cardview where I added the paddingBottom):
<ScrollView
android:id="#+id/item_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
tools:visibility="visible">
<LinearLayout
android:id="#+id/item_wrapper_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/content_margin"
android:paddingBottom="32dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/item_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/colorPrimary"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
Adding the LinearLayout wrapper around the cardview is what worked for me.
Also note, I had to add card_view:cardUseCompatPadding="true" on the cardview to get the border shadow looking correct.
Here is the end result where the red box shows where the padding has been added when the cardview is expanded and scrolled up.
Setting clipToPadding to false on a ScrollView usually works for me:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingBottom="16dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:contentPadding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Lorem ipsum..." />
</com.google.android.material.card.MaterialCardView>
</ScrollView>
One solution I just found is to wrap CardView with a LinearLayout or RelativeLayout and set its padding. For example, If you want some drop shadow effect in cardView, lets say 8dp, you can set 4dp padding of your LinearLayout or RelativeLayout and 4dp layout_margin of CardView.
In my case I just have to change ScrollView with NestedScrollView to solve the problem.
FYI, my NestedScrollView is placed inside a fragment which is a child of CoordinatorLayout with appbar_scrolling_view_behavior set.
The best solution is adding View with marginTop at last
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...................
...................
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</ScrollView>
I had same issue. Moving margin from child to ScrollView worked for me
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_margin="8dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
...
</LinearLayout>
</CardView>
</LinearLayout>
I want to place a LinearLayout below a FrameLayout which contains a ListView, but the content of the LinearLayout is not shown.
I also tried this out with a surrounding LinearLayout instead of the RelativeLayout without success.
The FrameLayout cannot be changed to different layout type!
The LinearLayout below will be shown and hidden programmatically.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/framelayout_container" >
<ListView
android:id="#+id/content_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#FFFFFF"
android:dividerHeight="1dp"
android:padding="0dp" >
</ListView>
<LinearLayout
android:id="#+id/description_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white" >
<include
android:id="#+id/description_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/included_layout" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:id="#+id/below_container"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
android:background="#33b5e5" >
<include
android:id="#+id/below_layout"
layout="#layout/below_layout"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
Any ideas how to get this work?
The problem is your FrameLayout is set to match the parent width and height, so it will leave no room for the LinearLayout beneath it. You would need to revise it to use its layout_height set to wrap_content, and change the inner ListView to do the same.
I want to programatically add viwes to vertical orientation linearlayout within horizontalscrollview within Scrollview. Basic scheme of my layout is belowe:
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
android:fadingEdge="none">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- same header code -->
</LinearLaout>
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<LinearLayout
android:id="#+id/list_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"/>
</HorizontalScrollView>
</ScrollView>
Now if i'm adding programatically 5 views to list_view linearlayout, i get only one view, and free sapce below it. If i comment HorizontallScrollView like that:
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
android:fadingEdge="none">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- same header code -->
</LinearLaout>
<LinearLayout
android:id="#+id/list_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"/>
</ScrollView>
All views are show correct, but they aren't horizontall scrollable. Please give me same ideas how to solve this problem.
Answer is to change linearlayout's android:layout_width attribute to wrap_content within horizontalScrollview
My mind is a bit boggled... in the following XML, I would expect my LinearLayout/TextView to be the same size as my ScrollView. As you can see from my included image, this is not the case.
What is going on here? Why is my LinearLayout not the same size as the ScrollView?
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#008B8B" >
<LinearLayout
android:id="#+id/contentLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DC143C" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello" />
</LinearLayout>
</ScrollView>
use
android:fillViewport="true"
in your ScrollView
I want the content inside the scrollView as center.
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay"
android:layout_gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:gravity="center_vertical|center_horizontal"/>
</ScrollView>
Note: there is no android:gravity attribute for scrollvew.
any sol:-
I had the same issue and finally figured it out. This is for a vertical ScrollView.
Put your ScrollView inside a RelativeLayout and center it in the RelativeLayout. In order for this to work, your ScrollView should have
android:layout_height="wrap_content"
This is how the final code should look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b2"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b3"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
How about this?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
</ScrollView>
I think a more elegant solution would be to use the ScrollView's android:fillViewport property. A ScrollView is a little different in how it treats it's content view (can only have one), even if you set match_parent (fill_parent) to the ScrollView it won't give that much spacing to it's content view, instead the default behavior is for the ScrollView to wrap the content regardless of what you specify for that view. What android:fillViewport does is tell the ScrollViewto stretch its content to fill the viewport (http://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport). So in this case, your LinearLayout would be stretched to match the viewport and if the height goes behind the viewport then it will be scrollable which is exactly what you want!
The accepted answer won't work properly when the content extends beyond the ScrollView because it will still center the content view first causing it to cut off a portion of the view, and the ScrollView centered in another layout works but just doesn't feel right, besides I think it will also result in a lint error (useless parent or something along those lines).
Try something like this:
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay"
android:fillViewport="true">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check" />
</LinearLayout>
</ScrollView>
Just remember that the reason it is being centered here now is because of the android:gravity on the LinearLayout since the ScrollView will stretch the LinearLayout so keep that in mind depending on what you add to the layout.
Another good read on ScrollView although not about centering but about fillViewport is http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Use this attribute in ScrollView
android:fillViewport="true"
Example
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:fillViewport="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Your Code goes here-->
</RelativeLayout>
</ScrollView>
Make sure to use Single Child in ScrollView just like in above example which is Relativelayout.
For #Patrick_Boss - what stopped the content from cutting of in my application was to change the gravity and layout_gravity to center_horizontal for the LinearLayout.
It worked for me...but not sure if it will work for you.
Modification of #Ghost's answer -
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
</ScrollView>
One thing to consider is what NOT to set. Make certain your child controls, especially EditText controls, do not have the RequestFocus property set.
This may be one of the last interpreted properties on the layout and it will override gravity settings on its parents (layout or ScrollView).
using the scroll view inside the ConstraintLayout. It is worked for me
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Scroll view height should be wrap_content
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"