Search code examples
javaandroidandroid-listviewandroid-buttonandroid-dialogfragment

Button at the bottom of a long ListView in a DialogFragment can't be seen


IF the ListView has a lot of children and she needs to scroll i can't see the dismiss button at the end of the FragmentDialog. Here you can see the Dismiss Button: enter image description here

And in the long ListView you can't see at the bottom the Dismiss Button: enter image description here

dialog.xml:

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

<TextView 
    android:id="@+id/dialog_header_text_view_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/dialog_header_text_view_text"
    android:textSize="22sp"
    android:textColor="@color/opening_words_dialog_header_color"
    android:layout_marginBottom="15sp"
    android:textStyle="bold"
    />

<ListView 
    android:id="@+id/dialog_list_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/dialog_header_text_view_id"
    />

<Button 
    android:id="@+id/dialog_dismiss_button_id"
    android:layout_width="100sp"
    android:layout_height="50sp"
    android:text="@string/list_view_button_footer_text"
    android:layout_below="@+id/dialog_list_view_id"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>

CustomDialogFragment.java:

 public class CustomDialogFragmentYearsKnownLoveMails extends DialogFragment
{
    TextView listViewItemTextView;
    ArrayAdapter<String> arrayAdapter;
    ListView dialogListView;
    String[] items = new String[120];

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.dialog, container,false);

        setCancelable(false);

        listViewItemTextView = (TextView) rootView.findViewById(R.id.list_view_item_text_view_id);
        dialogListView = (ListView) rootView.findViewById(R.id.dialog_list_view_id);
        dissmissDialogButton = (Button) rootView.findViewById(R.id.dialog_dismiss_button_id);

        for (int i = 0;i < items.length;i++)
        {
            items[i] = "" + (i + 1);
        }

        arrayAdapter = new ArrayAdapter<String>(LoveMailsActivity.this, R.layout.list_view_row,R.id.row_text_view_id,items);
        dialogListView.setAdapter(arrayAdapter);

        dialogListView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id)
            {
                Toast.makeText(getActivity(), items[position], Toast.LENGTH_SHORT).show();
                loveEmailYearsKnownTextView.setText(items[position]);
            }
        });

        dissmissDialogButton.setOnClickListener(new View.OnClickListener() // Dismiss button click
        {
            @Override
            public void onClick(View v) 
            {
                dismiss();
            }
        });

        return rootView;
    }
}

So what's wrong with it? if you need any more info just ask.


Solution

  • The list view, by the constraints above and below, is "stretch" from the bottom (i.e. base line) of the text view to the top of the button. Usually I start to write the component which touch the parent view (i.e. align parent top/bottom) and finish with the view in the middle as this view list.

    The trick is to expand the list below the textview and above the button like this:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/dialog_header_text_view_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dialog_header_text_view_text"
            android:textSize="22sp"
            android:textColor="@color/opening_words_dialog_header_color"
            android:layout_marginBottom="15sp"
            android:textStyle="bold"
            />
    
        <Button
            android:id="@+id/dialog_dismiss_button_id"
            android:layout_width="100sp"
            android:layout_height="50sp"
            android:text="@string/list_view_button_footer_text"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            />
    
        <ListView
            android:id="@+id/dialog_list_view_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/dialog_header_text_view_id"
            android:layout_above="@id/dialog_dismiss_button_id"
            />
    </RelativeLayout>