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.
Related
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>
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.
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>
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>
I am trying to place an ImageView in the middle of the screen, with the aim of putting other views above and below it so I have decided to use a LinearLayout (vertical) and a LinearLayout (horizontal). However, when I use layout_weight for the ImageView, the image disappears completely (a blank screen is shown). The ImageView's image is set in onCreate(). I would like the ImageView to take up 50% of the width of the screen, hence my use of weights.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_weight="2">
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ImageView android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/albumArtImageView"></ImageView>
<View android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
For layout_weight to be useful for anything, there needs to be left-over space. Your first view under the top LinearLayout has layout_height="fill_parent". That takes up all the room. Try setting layout_height="0dp" everywhere that you want the dimension determined by the layout_weight and I think your results will be better.
try to change the orientation of second linearlayout to vertical