ViewPager not shown after setting layout_weight="1" - android

I have a layout as below
<LinearLayout
android:id="#+id/home_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/slide_pager"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:visibility="visible" >
< .. views />
</RelativeLayout> </LinearLayout>
When I am setting the height of RelativeLayout footer to say 200dp , I see the ViewPager .If the footer is 'wrap_content' the ViePager is fully disappeared. I tried setting weight of ViewPager to 1 but of no use . I want the footer to be wrap_content. and ViewPager to occupy the remaining space.
thank you.
The issue is same even after setting layout_height to wrap_content.

If in a vertical LinearLayout, you set the width to 0dp, then it is not going to shown. Also happens in a horizontal LinearLayout and 0dp of height.
Just change the width to wrap_content (or whatever else).

The problem is that we have to supply the weight to child layouts according to its requirement. So I set weight for viewpager to .75 and to remaining layout to .25 and now the view pager will be displayed to 3/4th of the screen.
<LinearLayout
android:id="#+id/home_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/slide_pager"
android:layout_width="match_parent"
android:layout_weight=".75"
android:layout_height="0dp" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight=".25"
android:visibility="visible" >
< .. views />
</RelativeLayout> </LinearLayout>

Related

Right way to have a scrollable bottom view

What is the right way to have a scrollable bottom view?
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/wrnTextP1"
android:text="Ok , batatas "/>
<android.support.v4.widget.Space
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
<Button
style="#style/FullWidthButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Chega por hoje!" />
</LinearLayout>
at the preview its looke fine, but when I run it on the emulator the view(in this case: the button) its not at bottom at all.
I think you are misunderstanding how ScrollView is meant to be used. The LinearLayout child of your ScrollView has a match_parent height... if your ScrollView is the same height as the screen, and the LinearLayout is the same height as the ScrollView, then there's nothing to scroll (all the content is the size of the screen).
The direct child of a ScrollView should either have a fixed height (like 2000dp) or use wrap_content.
If you use a fixed height, then the rest of your code "works"; you can have a Space element that uses layout_weight to push the last view down to the bottom of your LinearLayout. However, if you're using wrap_content, this is impossible (since there's no "extra" space for the layout_weight to consume).
Instead, you could consider putting the "bottom" view outside of the ScrollView. Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
...
</ScrollView>
<Button
style="#style/FullWidthButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chega por hoje!" />
</LinearLayout>

HorizontallScrollView with auto-fitting pages

I am trying to create a HorizontalScrollView which will be exactly 3 times the screen width. I have tried the following code, but the scrollview will not scroll. How can I set it so that the 3 LinearLayouts inside the main LinearLayout are each the width of the screen?
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<!-- the container for all the pages -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FF0000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FF0000"/>
</LinearLayout>
Note that if I give the LinearLayouts a specific width, the scrollview does scroll, but I need each LinearLayout to have the width of the screen.
You will need to use wrap_content on your top LinearLayout, and set the width of the child LinearLayouts programmatically.

FrameLayout inside of ScrollView

I'm new to android development and I have the following problem.
I need to use FrameLayout inside of ScrollView for making it scrollable. I wrote this
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#00ff00">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#ff0000">
</FrameLayout>
</ScrollView>
But this doesn't work. I tried in RelativeLayout and it worked, but I need to use for FrameLayout.
Any ideas?
android:fillViewport="true" solves this issue.
android:fillViewport, Defines whether the scrollview should stretch its content to fill the viewport.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</ScrollView>
I tried many variants for solving this problem, including answers here, but no one was helpful. ScrollView always wraps FrameLayout, but everything is normal with RelativeLayout, LinearLayout...
I have found one solution for this - to set minimum height of FrameLayout. You can set dynamically minimum height by setMinimumHeight() method.
To get FrameLayout inside ScrollView, do this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayoutHeader1"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="1440dp"
>
</FrameLayout>
</LinearLayout>
</ScrollView>
Your xml sample is quite good, the only thing I can see is : if the ScrollLayout parent is bigger than 500dp it won't scroll.
Indeed, the scroll is used only if the content is bigger than the scrollview.
here, you set your content height to 500dip and you scrollview to 'match_parent'
if the parent of this scrollView take the whole screen, on a 800dip height screen (for example)
=> the scroll is simply not needed.
Try setting layout_height to "wrap_content" in your FrameLayout
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000">
It will scroll when the FrameLayout will be higher than the screen border. Or if you want the fixed height of scrollable area, set height to 500dp on ScrollView. It depends what you want, but don't ever set the fixed height on a ScrollView child. The ScrollView child's height should always be wrap_content.
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#00ff00">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000">
</FrameLayout>
</ScrollView>

Undesired FrameLayout Stretch to Wrap All Children's Heights (Instead of Respecting Just One Child's Height)

Basically, I am using a popular tactic to center a background image for a LinearLayout by wrapping it in a FrameLayout and adding an ImageView that uses fill_parent before the LinearLayout. However, when I add a ScrollView in the game, everything changes.
The issues is that the image is quite large vertically and the ScrollView respects the height of both the ImageView and the LinearLayout due to the wrap_content height attribute. This causes an undesired blank space below the LinearLayout when the image is vertically larger than it.
How do I make the FrameLayout's height stretch only to the child LinearLayout's height? If possible, without mangling with its sizes programmatically on run-time/draw-time.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/cash_bg" />
<LinearLayout
android:id="#+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<!-- My main views are here -->
</LinearLayout>
</FrameLayout>
</ScrollView>
I had the same problem and gone nuts searching for an answer. I think this is an Android bug. What I have done is to include the FrameLayout into a LinearLayout to be able to force "fill_parent" on FrameLayout's height, which increases the complexity, but solves the problem. Did not find any other (better) solution.
Try this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/cash_bg" />
<LinearLayout
android:id="#+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<!-- My main views are here -->
</LinearLayout>
</FrameLayout>
</LinearLayout>
</ScrollView>

View background color ignored

I have this (part of a) layout:
<ScrollView android:layout_width="0dip" android:layout_height="fill_parent"
android:layout_weight="0.8">
<HorizontalScrollView android:id="#+id/paint_board_container"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<View android:layout_width="wrap_content" android:layout_height="fill_parent"
android:background="#android:color/white"/>
</HorizontalScrollView>
</ScrollView>
The background color of the innermost view is set to white, but I can't see it on the screen, even if its layout width is also set to fill_parent. Where is the problem?
Thanks.
Updated layout:
<ScrollView android:layout_width="0dip" android:layout_height="fill_parent"
android:layout_weight="0.8" android:fillViewport="true"
android:background="#00FF00">
<HorizontalScrollView android:id="#+id/paint_board_container"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fillViewport="true"
android:background="#FF0000">
<View android:layout_width="2000dip" android:layout_height="1000dip"
android:background="#android:color/white"/>
<!-- android:minWidth="100dip" android:minHeight="100dip"/> -->
</HorizontalScrollView>
</ScrollView>
Now I see the vertical scroll view, but not the horizontal one, even though the innermost view's width is set to 2000dip. How do I make the horizontal scroll view show?
A View by default has width and height of 0. Since the HorizontalScrollView has a height of wrap_content, it will set its height to 0 as well. Give the inner view a non-zero minimum width and height and you should see some white on the screen.

Categories

Resources