ScrollView height doesn't fit to its content - Android Java

545 views Asked by At

I have a ScrollView and I want to add 2 images on it.
I created a ScrollView and then a LinearLayout. Then created 2 ImageViews and added them on LinearLayout. Then added LinearLayout on ScrollView. (All of steps by java code. Didn't use XML at all)
But the problem is the ScrollView height is not fitted to height of my images(It's too long).
This is my code:

LinearLayout scrollParent = new LinearLayout(this);
    scrollParent.setOrientation(LinearLayout.VERTICAL);
    scrollParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    helpView = new ScrollView(this);
    helpView.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    helpView.setFillViewport(true);

    inside = new LinearLayout(this);
    inside.setOrientation(LinearLayout.VERTICAL);
    inside.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    helpView.addView(inside);

    ImageView img = new ImageView(this);
    img.setImageResource(R.drawable.learn1);
    img.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));



    ImageView img2 = new ImageView(this);
    img2.setImageResource(R.drawable.learn2);
    img2.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));


    inside.addView(img);
    inside.addView(img2);
    scrollParent.addView(helpView);

Thanks for your help.

2

There are 2 answers

12
Woz On

Try this...

activity_demo.xml

<LinearLayout
    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:orientation="vertical"
    android:id="@+id/myLinear"
    tools:context="com.example.ricardo.demo.Demo">


</LinearLayout>

Demo.java

package com.example.ricardo.demo;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;


public class Demo extends Activity {

    private ScrollView helpView = null;
    private LinearLayout inside = null, myLinear = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);

        myLinear = (LinearLayout) findViewById(R.id.myLinear);
        showImages();
    }

    private void showImages(){
        helpView = new ScrollView(this);
        helpView.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        helpView.setFillViewport(true);

        inside = new LinearLayout(this);
        inside.setOrientation(LinearLayout.VERTICAL);
        inside.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        helpView.addView(inside);

        ImageView img = new ImageView(this);
        img.setImageResource(R.drawable.image1);
        img.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        inside.addView(img);

        ImageView img2 = new ImageView(this);
        img2.setImageResource(R.drawable.image2);
        img2.setLayoutParams(new ScrollView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        img2.setScaleType(ImageView.ScaleType.FIT_XY);
        inside.addView(img2);

        myLinear.addView(helpView);

    }

}
2
yahya On

I think you specify wrong type of layoutParams, you should specify a layoutParam for the viewGroup which the child will be placed in. Try like this:

// I don't know which type of layout is the parent of scrollParent, 
// but you should set its layoutParams as well, from your code 
// i assume that is LinearLayout
LinearLayout scrollParent = new LinearLayout(this);
scrollParent.setOrientation(LinearLayout.VERTICAL);
scrollParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

helpView = new ScrollView(this);
helpView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
helpView.setFillViewport(true);

inside = new LinearLayout(this);
inside.setOrientation(LinearLayout.VERTICAL);
inside.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,
            ScrollView.LayoutParams.WRAP_CONTENT));

helpView.addView(inside);

ImageView img = new ImageView(this);
img.setImageResource(R.drawable.learn1);
img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));


ImageView img2 = new ImageView(this);
img2.setImageResource(R.drawable.learn2);
img2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

inside.addView(img);
inside.addView(img2);
scrollParent.addView(helpView);