86

I got an Activity with two Fragments (one list one normal). And the normal Fragment inflates a Scrollview containing a LineaLayout (vertical) and this layout contains TextViews. The ScrollView and layout_width and layout_height are match_parent, so I think the whole screen should be used. But on the bottom there is still a "gap". I hope you can help me.

ScrollView.xml

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/titlescreen_bg"
    android:orientation="vertical"
    android:paddingTop="60dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_headline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="60dp"
        android:paddingTop="60dp"
        android:textIsSelectable="false"
        android:textSize="@dimen/fontsize_slogan_titlescreen" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingBottom="30dp"
        android:paddingTop="30dp"
        android:textIsSelectable="false"
        android:textSize="@dimen/fontsize_slogan_titlescreen" />
</LinearLayout>

</ScrollView>

the fragment inflating this layout.

package wak.iage.layout;

import wak.iage.R;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MenuContentFragment extends Fragment
{
LinearLayout.LayoutParams   relativeParams  = new LinearLayout.LayoutParams(
                                                    LayoutParams.MATCH_PARENT,
                                                    LayoutParams.MATCH_PARENT);
LinearLayout                topLayout       = null;
TextView                    body            = null;
TextView                    head            = null;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.menu_content_main, container);
    return v;
}

public void changeText(String title, String content) {
    topLayout = (LinearLayout) getActivity().findViewById(
            R.id.LinearLayout1);
    head = (TextView) getActivity().findViewById(R.id.tv_headline);
    body = (TextView) getActivity().findViewById(R.id.tv_content);

    if (body == null) {
        topLayout.removeViews(1, topLayout.getChildCount() - 1);
        body = new TextView(getActivity().getApplicationContext());
        body.setPadding(0, 30, 0, 20);
        body.setTextColor(Color.BLACK);
        body.setTextSize(22);
        body.setGravity(Gravity.CENTER_HORIZONTAL);
        topLayout.addView(body, relativeParams);
    }

    body.setText(content);
    head.setText(title);
}

public void addGlossary() {
    if (body != null) {
        topLayout.removeView(body);
    }

    int i = 0;

    for (int id : GLOSSARY) {
        TextView glossary = new TextView(getActivity()
                .getApplicationContext());
        glossary.setText(getString(id));
        glossary.setTextColor(Color.BLACK);
        if (i % 2 == 0) {
            glossary.setTypeface(Typeface.DEFAULT_BOLD);
            glossary.setTextSize(22);
            glossary.setPadding(0, 10, 0, 10);
        }
        topLayout.addView(glossary, relativeParams);
        i += 1;
    }
}

public static final int[]   GLOSSARY    = {
        R.string.GlossaryAndroidOSTitle, R.string.GlossaryAndroidOSContent,
        R.string.GlossaryAppTitle, R.string.GlossaryAppContent,
        R.string.GlossaryCloudTitle, R.string.GlossaryCloudContent,
        R.string.GlossaryDonwloadTitle, R.string.GlossaryDonwloadContent,
        R.string.GlossaryFacebookTitle, R.string.GlossaryFacebookContent,
        R.string.GlossaryGPSTitle, R.string.GlossaryGPSContent,
        R.string.GlossaryHomescreenTitle,
        R.string.GlossaryHomescreenContent, R.string.GlossaryPasswordTitle,
        R.string.GlossaryPasswordContent, R.string.GlossaryRouterTitle,
        R.string.GlossaryRouterContent, R.string.GlossarySDTitle,
        R.string.GlossaySDContent, R.string.GlossayStandbyTitle,
        R.string.GlossayStandbyContent, R.string.GlossaryTabletTitle,
        R.string.GlossaryTabletContent, R.string.GlossaryTouchscreenTitle,
        R.string.GlossaryTouchscreenContent, R.string.GlossayWidgetsTitle,
        R.string.GlossayWidgetsContent, R.string.GlossayWLANTitle,
        R.string.GlossayWLANContent };
}

Thanks a lot.

Edit: Even the proble is already fixed with: android:fillViewPort="true", I want to show you the problem.

But I don't have enough reputation to post a picture. Sorry!

AST
  • 211
  • 6
  • 18
j0chn
  • 1,103
  • 1
  • 7
  • 13

5 Answers5

310

If i'm not mistaken, the ViewGroup's height (LinearLayout's height in your case), that is the (only) child inside a ScrollView, is always interpreted as wrap_content, since that content can be larger than the ScrollView's height (hence the scrollbars).

This also means that if the content is smaller, the ScrollView's content (child) may not necessarily stretch to fill the screen.

In order to visually help you fix this, we need to see a screenshot of your problem.

Maybe setting android:fillViewport="true" on the ScrollView will fix your issue:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
Steven Byle
  • 13,149
  • 4
  • 45
  • 57
Streets Of Boston
  • 12,576
  • 2
  • 25
  • 28
  • 1
    Regarding "that is the (only) child inside a ScrollView, is always interpreted as wrap_content, since that content can be larger than the ScrollView's height (hence the scrollbars)": Couly you point me to the source code which does this? –  Feb 29 '16 at 13:09
  • In my case, the child `LinearLayout` had enough content to take more than the screen space but still, the Scrollview was not filling the screen and leaving like 10% of the screen at the bottom. fillViewPort fixed it. I had been messing with the code since yesterday. You are the savior man! – T.M Mar 07 '18 at 07:30
  • Yeas fillViewport="true" is this amazing thing that streches scrollview to entire screen height and enables to set content in center of screen in scroll view – Michał Ziobro Nov 13 '18 at 11:33
  • `android:layout_height="match_parent"` should be `android:layout_height="wrap_content"` – Jimit Patel Dec 24 '18 at 12:57
11
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:fadeScrollbars="false"
    android:scrollbars="vertical" >

In your ScrollView add an attribute ie.

android:fillViewport="true"
Melquiades
  • 8,496
  • 1
  • 31
  • 46
Trojan Horse
  • 203
  • 2
  • 6
2
inflater.inflate(R.layout.menu_content_main, container);

should be

inflater.inflate(R.layout.menu_content_main, container, false);
alex
  • 6,359
  • 1
  • 23
  • 21
1

Replace ScrollView with NestedScrollView it will also solve the problem of nested scrolling.

<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.core.widget.NestedScrollView>
RUSHIKESH KARDE
  • 281
  • 2
  • 6
0

I had a similar problem and could only fix it with a Helper Class. I found the original code online and this is my implementation of it.

Java class:

public class ImageViewHelper extends android.support.v7.widget.AppCompatImageView {

    public ImageViewHelper(Context context) {
        super(context);
    }

    public ImageViewHelper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewHelper(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable d = getDrawable();
        if (d != null) {
            int w = MeasureSpec.getSize(widthMeasureSpec);
            int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
            setMeasuredDimension(w, h);
        }
        else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

XML:

<com.example.app.ImageViewHelper
    android:id="@+id/img"
    android:src="@drawable/start"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:adjustViewBounds="true" />
Community
  • 1
  • 1
Dan Oprean
  • 56
  • 7