Adding view to bottom of layout inside a scrollview

AndroidLayoutScroll

Android Problem Overview


So my layout looks basically like this:

<ScrollView>
    <RelativeLayout>
        <BunchOfViews/>
        <ImageView android:layout_alignParentBottom="true"/>
    </RelativeLayout>
</ScrollView>

I have the ScrollView so all of the layout always is visible no matter the height of the screen. The problem is that on a very high screen, I still want my ImageView to be at the bottom. However, a child of a ScrollView don't seem to have a defined bottom. The View is placed at the top of the layout. How can I solve this problem in a neat way?

Android Solutions


Solution 1 - Android

I ran into the same issue. I never could find a very pleasing solution, but here is how I did it. Maybe someone else has a better way, I hate adding layouts that don't do anything.

My hack was to add a dummy linearlayout at the bottom of the scrollview that has fill_parent to take up all the room and force the scrollview to fill the screen. Then add whatever component I want to that linearlayout.

Here is one of my layouts that does this:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="15px" >

        <!-- bunch of components here -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/spinner"
            android:layout_marginTop="5px"
            android:gravity="center_horizontal|bottom"
            android:paddingTop="2px" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20px"
                android:paddingRight="20px"
                android:text="Delete" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

Solution 2 - Android

I had the same issue and found this page:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

Basically, you set the ScrollView's android:fillViewport to true, which will allow the child view to expand to the same height as the ScrollView itself, filling out the space. You then just need to have one of the child controls' layout_height set to fill_parent and layout_weight to 1, causing that control to "spring" to fill the empty space.

Note that if the contents of the ScrollView are already tall enough to fill the ScrollView, the android:fillViewport has no effect, so the setting only kicks in when needed.

My final XML looks like similar to this:

<ScrollView
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:fillViewport="true">
	<LinearLayout
		android:orientation="vertical"
		android:layout_height="wrap_content">
		<LinearLayout
			android:layout_height="fill_parent"
			android:layout_weight="1">
			<!-- this expands to fill the empty space if needed -->
		</LinearLayout>

		<!-- this sits at the bottom of the ScrollView,
		getting pushed out of view if the ScrollView's
		content is tall enough -->
		<ImageView
			android:layout_height="wrap_content"
			android:src="@drawable/footer_image">
		</ImageView>
	</LinearLayout>
</ScrollView>

Solution 3 - Android

it seems that the linearlayout isn't necessary, all that is important is the fillViewPort. you could just use

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottomParent="true">

now that you have specified the relativelayout to be at least the size of the screen.

Solution 4 - Android

its work for me perfectly android:fillViewport="true"

Solution 5 - Android

Joel Malone's answer https://stackoverflow.com/questions/2417221/adding-view-to-bottom-of-layout-inside-a-scrollview/4258531#4258531 does that trick. My solution is almost the same, except that I use Space widget to do the work of filling the rest height inside the parent layout. Like this:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        >
        <android.support.v4.widget.Space
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
    <ImageView
        android:layout_height="wrap_content"
        android:src="@drawable/footer_image"
        />
    </LinearLayout>
</ScrollView>

Attention:

  • fill_parent in the other answers is noted as deprecated for a long time;
  • Compare to an empty LinearLayout to fill the rest of parent layout, I think Space is much more appropriate(it's designed to do that job.)
  • Finally, don't forget that important attribute at the beginning of ScrollView: android:fillViewport="true". They together make this trick.

Solution 6 - Android

just put this attribute android:fillViewport="true" to your ScrolView and you will got what you need

Solution 7 - Android

On your view that you want to be at the bottom use android:gravity="bottom"

Solution 8 - Android

From: http://code.google.com/p/k9mail/source/browse/k9mail/trunk/res/layout/account_setup_basics.xml?r=1314

This should help you:

<RelativeLayout
        android:layout_marginTop="-45dip" 
        android:padding="0dip"
        android:layout_alignParentBottom="true"
        android:gravity="bottom|right" 
        android:background="@android:drawable/bottom_bar"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <Button
            android:id="@+id/manual_setup"
            android:text="@string/account_setup_basics_manual_setup_action"
            android:minWidth="@dimen/button_minWidth"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginBottom="-4dip" 
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="false" 
            />
        <Button
            android:id="@+id/next"
            android:text="@string/next_action"
            android:minWidth="@dimen/button_minWidth"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:drawableRight="@drawable/button_indicator_next"
            android:layout_marginBottom="-4dip" 
            android:layout_alignParentRight="true"
            android:layout_centerVertical="false" 
            />
    </RelativeLayout>

Solution 9 - Android

ScrollView view = new ScrollView( this );
ScrollView.LayoutParams lps = new FrameLayout.LayoutParams( FILL_PARENT, FILL_PARENT, Gravity.CENTER );
LinearLayout layout = new LinearLayout( this );
// what ever you want in the Layout 
view.addView( layout, lps );

Solution 10 - Android

I tried alot to align the Scroll View to bottom of the screen but thats not possible according to this link. https://newbedev.com/how-do-i-align-views-at-the-bottom-of-the-screen

The way i found was to create a view with 1dp height and aa id (lets say android:id="@+id/bottomView") at the bottom of your XML page.

now just add these attributes to your scroll view..

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true"
        android:layout_alignBottom="@+id/bottomView"
        >

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpgsandstromView Question on Stackoverflow
Solution 1 - AndroiddweeboView Answer on Stackoverflow
Solution 2 - AndroidJoel MaloneView Answer on Stackoverflow
Solution 3 - AndroidLaurentView Answer on Stackoverflow
Solution 4 - AndroidShohel RanaView Answer on Stackoverflow
Solution 5 - AndroidAnthonyeefView Answer on Stackoverflow
Solution 6 - AndroidMostafa OnaizanView Answer on Stackoverflow
Solution 7 - Androidhitch45View Answer on Stackoverflow
Solution 8 - AndroidPentium10View Answer on Stackoverflow
Solution 9 - AndroidShiblyView Answer on Stackoverflow
Solution 10 - AndroiddankladView Answer on Stackoverflow