I want to make a ScrollView which includes a LinearLayout:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000000" >
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal" />
</ScrollView>
Now when i add children to mainHolder I cant scroll. whats wrong?
ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.
Adding a horizontal linear layout in a vertical scroll view is suspicious.
For horizontal, scrolling, use a HorizontalScrollView.
Also, wrap_content size in the scrolling direction is meaningless. If it really wrapped to content size, there would be no need to scroll.
try this one
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000000" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal" />
</LinearLayout>
</ScrollView>
you are adding the child to the linear layout which is horizontal in orientation
so it wrap the content horizontally
change the orientation of the linear layout to vertical or try Horizontal Scrollview
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical" />
Related
When I add this, the buttons I add programatically to the RelativeLayouts don't show up in the layouts. But when I change the width from 0 to fill parent, they overlap. How can I make each RelativeLayout take up half the width of the LinearLayout?
<ScrollView
android:id="#+id/svButtons"
android:layout_width="match_parent"
android:layout_height="175dp" >
<LinearLayout
android:id="#+id/linlayColumns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2">
<RelativeLayout
android:id="#+id/rellayLeftColumn"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/rellayRightColumn"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
</ScrollView>
If you want to split your RelativeLayouts horizontally, your LinearLayout orientation should be set to horizontal :
<LinearLayout
android:id="#+id/linlayColumns"
android:orientation="horizontal"
... >
I have the following HorizontalScrollView with 2 custom views.
For some reason it doesn't scroll
<com.name.CustomObjects.HorizontalLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:fillViewport="true"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.name.Views.AView
android:id="#+id/aView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.name.Views.BView
android:id="#+id/bView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</com.name.CustomObjects.HorizontalLayout>
Is there any content in either AView or BView? Because both their width is set to wrap_content, it's possible the ScrollView doesn't stretch...?
Have you extended the class ScrollView in your HorizontalLayout?
It may also be possible your HorizontalLayout doesn't stretch because of fillViewPort="true"
I simply add a ScrollView out of a LinearLayout, whatever I change their width it did not scroll at all. I am a newbie here, anyone help me out? thanks in advance!
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="1000dp"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/widget_box_action_1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/widget_box_action_2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/widget_box_action_3" />
...
</LinearLayout>
</ScrollView>
set android:layout_width="wrap_content" of linearlayout, not to 1000dp.
if u want to check why it is not scrolling try to set android:layout_height="100dip" for scrollview. the content which u want to display is enough that are easily shown on screen. If more content are there then scrollview will scroll. also set android:layout_width="fill_parent" of linearlayout instead of 1000dp set to fill_parent.
It looks like when you setup your ScrollView you are telling it to fill screen width with fill_parent. Try the below code. If I'm understanding correctly you want to scroll horizontally
<ScrollView
android:layout_width="1000dip"
android:layout_height="fill_parent"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="1000dip"
android:layout_height="fill_parent"
android:gravity="center" >
I want to create a Textview with a scrollview. But it doesn't work.
It's just a Textview, which is centered-
Here is the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="453dp"
android:layout_height="122dp"
android:gravity="center"
android:layout_marginLeft="40dp">
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:id="#+id/text1"
android:layout_width="453dp"
android:layout_height="122dp"
android:layout_gravity="left"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/black"
android:textSize="16dp"
android:maxLines="400"
/>
</ScrollView>
</LinearLayout>
fixed scroll view height
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:scrollbars="vertical"
android:fillViewport="true">
and make height of text View to wrap_content hope this will help
You have to set Text View property simply. Don't use ScrollView
android:scrollbars="vertical"
Try removing the LinearLayout and put it into the ScrollView.
So you have ScrollView on top level and in the ScrollView you put the LinearLayout. In the LinearLayout you got the TextView.
Your LinearLayout and the TextView are the same height. So there is now way the ScrollView will scoll, because the View inside isn't taller than the ScrollView itself (which can grow to the same dimensions as the LinearLayout outside).
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"