2

I am trying to make whole layout scrollable but in this case only listview is being scrolled and not the whole layout. Please tell me how can I make the whole page to be scrolled up and not just the listview in it. Thanks

  1. My XML file

    <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"
    tools:context=".MainActivity">
    
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <GridView
        android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="2dp"
        android:numColumns="auto_fit"
        android:paddingTop="2dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="2dp" />
    
    </LinearLayout>
    
    <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:text="More Devices"
    android:textStyle="bold" />
    
    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="3dp"
    android:paddingLeft="3dp"
    android:paddingTop="3dp" />
    
    </LinearLayout>
    
  2. My MainActivity.java

package project.example.com.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class MainActivity extends Activity {

GridView grid;
String[] gridtext = {"Text 1", "Text 2", "Text 3", "Text 4", "Text    5", "Text 6", "Text 7", "Text 8", "Text 9"};
int[] gridimage = {R.drawable.img, R.drawable.img, R.drawable.img,   R.drawable.img, R.drawable.img, R.drawable.img, R.drawable.img,   R.drawable.img, R.drawable.img};
ListView listView;
String[] listtext = {"List 1", "List 2", "List 3", "List 4", "List   4", "List 4"};
int[] listimage = {R.drawable.image, R.drawable.image,  R.drawable.image, R.drawable.image, R.drawable.image, R.drawable.image};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
CustomGrid adapter = new CustomGrid(MainActivity.this, gridtext,  gridimage);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener()    {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        Toast.makeText(MainActivity.this, "You Clicked at " +   gridtext[+position], Toast.LENGTH_SHORT).show();
    }
    });

    List<HashMap<String, String>> list = new  ArrayList<HashMap<String, String>>();

    String[] from = {"txt", "img"};
    int[] to = {R.id.list_text, R.id.list_image};
    SimpleAdapter simpleAdapter = new   SimpleAdapter(getBaseContext(), list, R.layout.list_row, from, to);
    for (int i = 0; i < listtext.length; i++) {
    HashMap<String, String> hashMap = new HashMap<String, String> ();
    hashMap.put("txt", listtext[i]);
    hashMap.put("img", Integer.toString(listimage[i]));
    list.add(hashMap);
    }

    listView.setAdapter(simpleAdapter);
    listView.setOnItemClickListener(new  AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int   position, long id) {
        Toast.makeText(MainActivity.this, "You Clicked at " +            listtext[+position], Toast.LENGTH_SHORT).show();
    }
    });
    View header = getLayoutInflater().inflate(R.layout.header,  null);
    listView.addHeaderView(header);


    }
    }
9
  • try scrollview .Just google it for examples Jul 23, 2015 at 12:00
  • 1
    Avoid like death putting a scrollable View, such as a ListView or GridView and derivates inside another scrollable View (i.e.: a ScrollView). Jul 23, 2015 at 12:03
  • 1
    Use a ScrollView and instead of the ListView use a LinearLayout to inflate your rows. Like someone said before me, avoid using a scrollable elements inside other scrollable elements if they scroll in the same orientation. You will run into problems. Jul 23, 2015 at 12:09
  • just add scrollview tag
    – Nauman Ash
    Jul 23, 2015 at 12:19
  • adding ScrollView tag would show just one row in GridView as well as ListView. Jul 24, 2015 at 6:50

5 Answers 5

0

In such a case you just have to add a ScrollView like this :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
.
.
.
</ScrollView>

But do remember to remove xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools", these two lines from your LinearLayout

4
  • Doing this would just show one row in GridView and ListView. How should i handle that? Jul 24, 2015 at 6:52
  • So @DivyamGarg ... for that.... Is it really necessary to implement both grid and list in the same activity... Let me know what exactly you are planning to do? Jul 24, 2015 at 7:12
  • I am trying to create an activity which has gridView items on the top, then a textview and then a list of items in a listView. The views are populated in the java file. Jul 24, 2015 at 11:51
  • For such a thing, I would suggest you to fix the size of ListView at the bottom and at top fix the size of GridView at top and in-between put your TextView; without putting any scollView Jul 26, 2015 at 5:12
0

You Should Put your Layout and Views Inside ScrollView

<LinearLayout>

    <ScrollView>

        <LinearLayout>
                  <GridView></GridView>
         </LinearLayout>
         <TextView></TextView>
         <LinearLayout>
                  <ListView></ListView>
         </LinearLayout>


    </ScrollView>
</LinearLayout>

Reference :How can I put a ListView into a ScrollView without it collapsing?

1
  • But ScrollView can have only one immediate child and not more than that. @Anil Meenugu Jul 24, 2015 at 6:49
0
        <ScrollView 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"             
        tools:context=".MainActivity" >

        <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <GridView
            android:id="@+id/grid"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
            android:columnWidth="100dp"
            android:gravity="center"
            android:horizontalSpacing="2dp"
            android:numColumns="auto_fit"
            android:paddingTop="2dp"
            android:stretchMode="columnWidth"
            android:verticalSpacing="2dp" />

        </LinearLayout>

        <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:text="More Devices"
        android:textStyle="bold" />

        <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="3dp"
        android:paddingLeft="3dp"
        android:paddingTop="3dp" />

        </LinearLayout>
          </ScrollView>
1
  • Doing this would show only one row in gridView and listView. Please help @Azim Ansari Jul 24, 2015 at 6:45
0

Add a ScrollView to your xml file that will contain your list view and other elements you would like to scroll. ScrollView can only hold one child view. In order to make the whole screen scrollable, you have to put a single layout inside the ScrollView that contains all of your other views. LinearLayout, GridLayout, RelativeLayout can all hold multiple child views(nest your layouts and views in a single parent layout). Just make the ScrollView the height and width of the entire screen and you will have it.

1
  • 1
    Your answer should be edited, the use hasn't implemented any scrollview to make the height and width match the entire screen. Instead of editing you can suggest him to create a new scrollview. Jul 23, 2015 at 12:02
0

I had exactly the same problem as you. My line of solving the problem started with what the user Touhid points out here:

Putting ListView inside a ScrollView is never inspired. But if you want your posted XML-like behavior, there're 3 options to me:

  1. Remove ScrollView: Removing your ScrollView, you may give the ListViews some specific size with respect to the total layout (either specific dp or layout_weight).

  2. Replace ListViews with LinearLayouts: You may add the list-items by iterating through the item-list and add each item-view to the respective LinearLayout by inflating the view & setting the respective data (string, image etc.)

  3. If you really need to put your ListViews inside the ScrollView, you must make your ListViews non-scrollable (Which is practically the same as the solution 2 above, but with ListView codes), otherwise the layout won't function as you expect. To make a ListView non-scrollable, you may read this SO post, where the precise solution to me is like the one below

:

In my case:

  1. solution 1 - remove ScrollView - was not an option. Creating ListViews with specific sizes was just bad looking and confusing.

  2. I wanted to try to Replace ListViews with LinearLayouts, but it turns out the first hit on google searching this suggested: it's not good idea, specially if your ListView contains a lot of items. It's better to add header to your ListView or use custom NonScrollableListView class. As headers looked bad also on my lists, I decided not to.

  3. So it seems the last option was for me. I did make my ListViews non scrollable. And it worked great. The answer I found was here. More precisely the answer by Dedaniya HirenKumar - answered Jul 8 '14 at 10:21. Be careful not to forget the last part of that answer. It is not enough to create the nonscrollable listview java class and to change the XML!!! You need to define your nonscrollable listviews in your Java file linked to the xml. Also be careful that you place the nonscrollable listview javaclass inside your package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.