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);


    }
    }
  • try scrollview .Just google it for examples – Sunil Sunny Jul 23 '15 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). – Phantômaxx Jul 23 '15 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. – Daniel Julio Jul 23 '15 at 12:09
  • just add scrollview tag – Nauman Ash Jul 23 '15 at 12:19
  • adding ScrollView tag would show just one row in GridView as well as ListView. – Divyam Garg Jul 24 '15 at 06:50
  • please provide me with some code @DanielJulio – Divyam Garg Jul 24 '15 at 06:50
  • @DivyamGarg The method I stated works well if you don't have many list and grid items. How many do you have? If you have many you will need a different way because the rows won't be recycled. – Daniel Julio Jul 24 '15 at 07:57
  • @DanielJulio I have a 3x3 gridView and 4-5 items in a listView – Divyam Garg Jul 24 '15 at 11:53
  • @DivyamGarg I'm not going to get an example of this, but if I was you I would use a ScrollView with a LinearLayout (this would be your listview) and a TableLayout (for the gridview). Then you would loop through how many rows you have and inflate your row layout. Something like this to inflate the row: `LinearLayout mLl = (LinearLayout) inflate(context, R.layout.row, this);`. After this you simply add your row to LinearLayout or TableLayout using `linearLayout.addView(mLl);` – Daniel Julio Jul 24 '15 at 12:10

5 Answers5

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
    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. – Prokash Sarkar Jul 23 '15 at 12:02
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

  • Doing this would just show one row in GridView and ListView. How should i handle that? – Divyam Garg Jul 24 '15 at 06: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? – Aditya Prasun Jul 24 '15 at 07: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. – Divyam Garg Jul 24 '15 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 – Aditya Prasun Jul 26 '15 at 05:12
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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Azim Ansari
  • 1,378
  • 11
  • 20
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?

Community
  • 1
  • 1
Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
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.

Adrian Stoica
  • 436
  • 5
  • 6