jason
jason

Reputation: 7164

ScrollView is not working in Android app

I have this layout :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/root_ll"
        android:orientation="vertical"
        android:gravity="center_horizontal">

    </LinearLayout>

</ScrollView>

And this code-behind below, everything is generated perfectly but the screen is not scrollable, can you tell me what the problem is? Thanks.

LinearLayout linearLayout= (LinearLayout) findViewById(R.id.root_ll);
for(int i = 0; i < 6 i++)
{
            LinearLayout llInside= new LinearLayout(this);
            llInside.setOrientation(LinearLayout.HORIZONTAL);
            llInside.setLayoutParams(new AbsListView.LayoutParams(
                    AbsListView.LayoutParams.WRAP_CONTENT,
                    AbsListView.LayoutParams.WRAP_CONTENT));
            WebView wView = new WebView(this);


            TextView tvInside = new TextView(this);
                    wView.loadUrl("file:///android_asset/a.gif");
                    setContentView(wView);
                    tvInside.setText("A");

 wView.setLayoutParams(new AbsListView.LayoutParams(
                    600,
                    600));

            tvInside.setTextSize(TypedValue.COMPLEX_UNIT_SP, 74);

            if(wView.getParent()!=null)
                ((ViewGroup)wView.getParent()).removeView(wView);

            if(tvInside.getParent()!=null)
                ((ViewGroup)tvInside.getParent()).removeView(tvInside);

            llInside.addView(wView);
            llInside.addView(tvInside);




            if(llInside.getParent()!=null)
                  ((ViewGroup)llInside.getParent()).removeView(llInside);

            linearLayout.addView(llInside);


            // linearLayout.addView(wView);
            //make visible to program



}
    if(linearLayout.getParent()!=null)
            ((ViewGroup)linearLayout.getParent()).removeView(linearLayout);
        setContentView(linearLayout);

Upvotes: 0

Views: 110

Answers (5)

Pramod Gharu
Pramod Gharu

Reputation: 1101

You have use:

android:layout_height="fill_parent";

And for ScrollView use:

android:layout_height="wrap_content";

Then its work.

Upvotes: 2

Mike M.
Mike M.

Reputation: 39191

Your calls to setContentView() are the problem. That method sets the layout for the Activity. Every time your code calls it, it's completely replacing the Activity's content, and your ScrollView is long gone. Remove all calls to setContentView() in the posted code, as well as the following:

if(linearLayout.getParent()!=null)
    ((ViewGroup)linearLayout.getParent()).removeView(linearLayout);

You can also remove the null checks on the parent Views for the Views you're creating dynamically, as they won't have parents until you add them.

LinearLayout linearLayout= (LinearLayout) findViewById(R.id.root_ll);
for(int i = 0; i < 6; i++)
{
    LinearLayout llInside= new LinearLayout(this);
    llInside.setOrientation(LinearLayout.HORIZONTAL);
    llInside.setLayoutParams(new AbsListView.LayoutParams(
                                 AbsListView.LayoutParams.WRAP_CONTENT,
                                 AbsListView.LayoutParams.WRAP_CONTENT));

    WebView wView = new WebView(this);

    TextView tvInside = new TextView(this);
    wView.loadUrl("file:///android_asset/a.gif");

    tvInside.setText("A");

    wView.setLayoutParams(new AbsListView.LayoutParams(
                              600,
                              600));

    tvInside.setTextSize(TypedValue.COMPLEX_UNIT_SP, 74);

    llInside.addView(wView);
    llInside.addView(tvInside);

    linearLayout.addView(llInside);
}

Upvotes: 3

Parth Bhayani
Parth Bhayani

Reputation: 1924

Try this, it's working

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/root_ll"
        android:orientation="vertical"
        android:gravity="center_horizontal">

         <TextView
            android:id="@+id/txtHeaderTitle"
            android:layout_width="wrap_content"
            android:layout_height="1000dp"
            android:text="Hello World"
            android:textColor="@android:color/black"
            android:textSize="20sp" />

    </LinearLayout>

</ScrollView>

Upvotes: 0

Ranjit
Ranjit

Reputation: 5150

Just change a little in heights only..just make all to wrap_content

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/root_ll"
    android:orientation="vertical"
    android:gravity="center_horizontal">

</LinearLayout>

And make sure the whole content height is more than screen height.

EDIT

As WebViews are scrollable. You just use LinearLayout only..No need to use ScrollView here.

Upvotes: 0

MurugananthamS
MurugananthamS

Reputation: 2405

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_ll2"
    android:gravity="center_horizontal">


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

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_ll"
    android:orientation="vertical"
    android:gravity="center_horizontal">

 your buttons ,etc...

</RelativeLayout>

Upvotes: 0

Related Questions