How to scroll a LinearLayout horizontally? - android

I have a LinearLayout that I do larger than the screen. I want to scroll that in horizontal. I have the next code but it doesn't work. Can anybody help me?
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/gm_movimientos"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>

Your HorizontalScrollView width needs to be set to either match_parent or a fixed size. Right now the ScrollView is expanding to the same dimensions of the LinearLayout (i.e. its bounds are also going off of the screen).

Related

Android center children in HorizontalScrollView

I have a HorizontalScrollView with an ImageView and a RelativeLayout on top of the ImageView. The RelativeLayout gets multiple ImageButtons created and assigned on runtime dynamically.
<HorizontalScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
<ImageView
android:id="#+id/scene"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:contentDescription="#string/indoor_tour_scene_view"
android:adjustViewBounds="true"
>
</ImageView>
<RelativeLayout
android:id="#+id/coordinate_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignTop="#id/scene"
>
<!-- Mapstops will be added programmatically -->
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
In portrait mode, the ImageView fills the view port and HorizontalScrollView in height, and overlaps in width so you have to scroll left right to see everything.
In landscape mode, the height is still 100% but the width will be less than the viewport due to image scale. So I want to center the views within the HorizontalScrollView.
I experienced some problems with offsets when I used layout_gravity="center" on the RelativeLayout. I found this blog entry https://content.pivotal.io/blog/centering-a-view-within-a-scrollview that says its an android bug, and I added the workaround as described there. It works in terms of the offset, but now my RelativeLayout #+id/coordinate_container fills the entire frame. That is too large and should actually match the size of the ImageView, because the dynamically created ImageButton positions are relative to the ImageView.
How can I have the child of the HorizontalScrollView centered without the offset bug, but also have the coordinate_container relative to the content of the ImageView, so it does not fill the entire frame?

Scrollview doesn't scroll after resizing it's child using animation

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>

ScrollView goes on top of another layout Android

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.

Custom View inside HorizontalScrollView not scrolling

I have a custom view inside a HorizontalScrollView like this:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<com.mina.demo.customwidgets.MyCustomView
android:layout_width="wrap_content"
android:layout_height="50dp"
/>
</HorizontalScrollView>
In MyCustomView onDraw() method, I draw some text and bitmaps.
the problem is that the width of the custom view becomes larger than the screen width, and wrapping horizontal scroll view does not scroll as if it is disabled.
what can be the reason for this ?
If your MyCustomView is always larger than the screen width, then it's safe to set the width of HorizontalScrollView equal to its contents. Try the following:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<com.mina.demo.customwidgets.MyCustomView
android:layout_width="wrap_content"
android:layout_height="50dp"
/>
</HorizontalScrollView>
I think the problem right now is that the width of the HorizontalScrollView is fixed at fill_parent. The scrolling is enabled by default and you haven't manually changed it so that's not the problem.

Positioning ListViews inside FrameLayout

So I'm trying to create a screen which has a ListView and over that I need to be able to float another custom horizontal ListView, right at the bottom edge of the screen. When the user scrolls on the vertical listview, the horizontal one would go invisible and reappear when the scrolling stops. I figured FrameLayout would be my best bet for overlapping views. But I can't seem to make this work. The Horizontal listview seems to occupy the whole screen space. Any ideas? Is this even the right approach? I wish to have something similar to a fixed div in HTML.
Here's my XML:
UPDATE-1: Used RelativeLayout as suggested, but still a no-go. The HorizontalListView still seems to be occupying the whole screen. I'm using the HorizintalListView from here
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="#+id/messages"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dip" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<test.ui.app.HorizontalListView
android:id="#+id/folders"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
</RelativeLayout>
</RelativeLayout>
I got it to work by setting the height of the inner Relative Layout myself instead of using 'wrap_content'.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="#+id/messages"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="80dip"
android:layout_alignParentBottom="true" >
<test.ui.app.HorizontalListView
android:id="#+id/folders"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
</RelativeLayout>
</RelativeLayout>
You cannot adjust the views inaide FrameLayout.So it will be better for you to go for RelativeLayout.
Or you can put your listviews inside RelativeLayout or linearlayout and then you can adjust.
Hope this will help you. :)
Like the other answerer said, you could use a RelativeLayout:
set android:layout_alignParentLeft|Right|Top|Bottom="true" for the vertical list view
set android:layout_alignParentLeft|Right|Bottom="true" for the horizontal list view (and height to "wrap_content" or fixed size)
Or if you reeeeaaaally want to stick with FrameLayout (maybe for performance reasons...), you could somply add a huge android:layout_marginTop to the horizontal list view. But this solution is uglier, since you need to set exact values. For example if the whole screen is 320dp height, and you want the horizontal list view to be 80dp height, you need to set the top margin to 240dp. However if you run this on a screen with different aspect ratio, the horizontal list view will be ugly.

Categories

Resources