Home » Android » android – Scrollview makes bottom views disappear in a Dialog

android – Scrollview makes bottom views disappear in a Dialog

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a dialog that has a list (a bunch of TextViews inside a LinearLayout) inside a ScrollView. The layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/delete_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/filename_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/filename_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <View
        android:id="@+id/horisontal_separator"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:id="@+id/button_container"
        android:layout_width="match_parent"
        android:layout_height="48dp" >

        <Button
            android:id="@+id/load_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/button_load"
            android:gravity="center"
            android:layout_weight="1" />

        <View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="@android:color/darker_gray" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/button_delete"
            android:gravity="center"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

It looks like this with only a few items on the list:
A few items on the list

But when there’s more than can fit on the screen (and there’s actual need to scroll), my buttons get pushed below the screen. When scrolling all the way to the bottom, it looks like this:
A lot of items on the list

I need the LinearLayout containing the buttons to stay as footer, it shouldn’t scroll anywhere and obviously not disappear. I’ve tried fiddling with the layout heights and weights, but to no avail.

How to&Answers:

try to change in scrollView

<ScrollView
    android:id="@+id/filename_scroll"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp" >

Answer:

I think you should mention the Scrollview’s layout_height explicitly like

<ScrollView
    android:id="@+id/filename_scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" > ////say  android:layout_height="50dp"

    <LinearLayout
        android:id="@+id/filename_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

like that. And another option is, introduce an outer component like table layout by specifying the layout_height explicitly and include your scrollview as it is.

Hope it works

Answer:

Use below xml layout for custom dialog that you want to display,

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/gameList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <Button
            android:id="@+id/load"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Load" />

        <Button
            android:id="@+id/delete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Delete" />
    </LinearLayout>
</FrameLayout>

And call it like this from your class file,

>     Button getList = (Button) findViewById(R.id.getList);
>     getList.setOnClickListener(new View.OnClickListener() {
>       
>             @Override
>       public void onClick(View v) {
>       // TODO Auto-generated method stub
>       final Dialog listDialog = new Dialog(MainActivity.this);
>       listDialog.setTitle("Load a game");
>       listDialog.setContentView(R.layout.custom_dialog);
>     
>       ListView gameList = (ListView) listDialog.findViewById(R.id.gameList);
>       gameList.setAdapter(new ArrayAdapter<String>          (MainActivity.this,android.R.layout.simple_list_item_1,new String[] {"your array" }));
>       
>       Button dismiss = (Button) listDialog.findViewById(R.id.load);
>       dismiss.setOnClickListener(new View.OnClickListener() {
>     
>           @Override
>           public void onClick(View v) {
>               // TODO Auto-generated method stub
>               listDialog.dismiss();
>                       }
>           });
>     
>           listDialog.show();
>       
>            } });