Reputation: 1945
I have an xml file that contains a ScrollView
and inside there is a LinearLayout
; this layout also contains 2 more layouts:
1st is where data will be added dynamically
2nd is a just something that needs to be displayed at the bottom of the view.
But gravity isn't working, I've tried both gravity
and layout_gravity
but none work, all layouts are set to fill the parent so why is this happening?
The copyright layout must be inside the ScrollView
but at the bottom of the screen even if ScrollView
is empty (1st layout).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/istoricLayout"
android:visibility="visible" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/istoric"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" //here I'm trying to move this at the buttom
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/thick_bar_height"
android:layout_marginBottom="@dimen/bar_vertical_space"
android:layout_marginTop="@dimen/bar_vertical_space"
android:background="@color/white" />
<Button
android:id="@+id/versiuneaCompleta"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/color2"
android:text="Vizioneaza versiunea completa"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="15dp" />
<RelativeLayout
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/white" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:src="@drawable/support_right_icon" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:src="@drawable/support_left_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/white"
android:text="Support +40.XXX.XXX.XXX"
android:textAlignment="center"
android:textColor="@color/color2"
android:textSize="15dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Politica de condidentialitate ·"
android:textColor="@color/white"
android:textSize="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Despre noi ·"
android:textColor="@color/white"
android:textSize="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Contact"
android:textColor="@color/white"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="© 2013 Artcos Design. All Rights Reserved"
android:textColor="@color/copyRightColor"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Upvotes: 2
Views: 4819
Reputation: 51
Check out this link, it contains the explanation for your problem:
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
The android:fillViewport of the ScrollView should be set to true which allows the child view to expand itself to fill the ScrollView's height. Then, pick one child element that is going to be expanded in order to fill the empty space and set its layout_weight to 1 (assuming LinearLayout is used). That element is usually the one right above the widget that is intended to be placed right at the bottom.
The XML file should look something like this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content">
<!-- widget 1 -->
<!-- widget 2 -->
<!-- ... -->
<!-- widget n -->
<!-- this widget expands to fill the empty space -->
<TextView
android:layout_height="fill_parent"
android:layout_weight="1">
</TextView>
<!-- this copyright layout is going to be placed rock bottom -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="© 2013 Artcos Design. All Rights Reserved"
android:textColor="@color/copyRightColor"
android:textSize="10dp" />
</LinearLayout>
</ScrollView>
Upvotes: 2
Reputation: 14710
Change the whole thing to:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/istoricLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/thick_bar_height"
android:layout_marginBottom="@dimen/bar_vertical_space"
android:layout_marginTop="@dimen/bar_vertical_space"
android:background="@color/white" />
<Button
android:id="@+id/versiuneaCompleta"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/color2"
android:text="Vizioneaza versiunea completa"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="15dp" />
<RelativeLayout
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/white" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:src="@drawable/support_right_icon" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:src="@drawable/support_left_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/white"
android:text="Support +40.766.154.894"
android:textAlignment="center"
android:textColor="@color/color2"
android:textSize="15dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Politica de condidentialitate ·"
android:textColor="@color/white"
android:textSize="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Despre noi ·"
android:textColor="@color/white"
android:textSize="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Contact"
android:textColor="@color/white"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="© 2013 Artcos Design. All Rights Reserved"
android:textColor="@color/copyRightColor"
android:textSize="10dp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomLayout" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/istoric"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
EDIT Setting referenced dimensions, colors and images to something meaningful, the layout looks something like below image (for clarity, the ScrollView has a red background and bottomLayout has a yellow background).
As you can see bottomLayout
is on the bottom.
Upvotes: 2
Reputation: 152797
Based on the comments to @Doomsknight's answer, set android:fillViewPort="true"
on your ScrollView
.
Height attribute on scroll view's child is ignored (if it was respected, the whole scroll view would be useless). Setting fillViewPort="true"
makes the scroll view child expand to parent height if it is less than parent height and otherwise the attribute does nothing. This way your child LinearLayout
in the scroll view will be at least as tall as the scroll view's parent, making the gravity work the way you want.
In addition, make the child LinearLayout
heights to wrap_content
and assign the first LinearLayout
a non-zero layout_weight
. You can also remove the bottom
layout_gravity
.
Upvotes: 2
Reputation: 13855
Move your textview, below the scrollview. You are using gravity in the wrong manner.
I assume you want your copyright at the bottom fixed.
Gravity is used to display contents of the view, in certain ways. or how to position the view itself, but it will not change the view hierarchy.
</LinearLayout>
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="© 2013 Artcos Design. All Rights Reserved"
android:textColor="@color/copyRightColor"
android:textSize="10dp" />
</LinearLayout>
you will also need to change the scroll view, so it does not take the screen height, but instead will best fill the area leaving room for copyright notice:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
Upvotes: 1