I got an Activity
with two Fragment
s (one list one normal).
And the normal Fragment
inflates a Scrollview
containing a
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.
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);
}
}
<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" />
inflater.inflate(R.layout.menu_content_main, container);
should be
inflater.inflate(R.layout.menu_content_main, container, false);
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">
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>
<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"