Android – DialogFragment with extra space below ListView

androidandroid-fragmentsdialog

As you can see, below the bottom list element in my ListView, there is excess space I can't seem to be rid of. I've tried Relative and Linearlayout, both look like this. Here's the code:
enter image description here

public class ChooseDialog extends DialogFragment implements
        DialogInterface.OnClickListener {

    String URLhome;
    String Title;
    String type;

/*  public static ChooseDialog newInstance() {
        ChooseDialog dialog = new ChooseDialog();
        Log.v("a", "shit runs");
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }*/

    public ChooseDialog(String type) {
        this.type = type;
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setCancelable(true);
        int style = DialogFragment.STYLE_NORMAL, theme = 0;
        setStyle(style, theme);
    }

     @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(type);
            builder.setNegativeButton("Cancel", this);
            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View dialogLayout = inflater.inflate(R.layout.dialog, null);
            builder.setView(dialogLayout);

            final String[] items = {"Red", "Green", "Blue" };

            builder.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items), 
                    new DialogInterface.OnClickListener() {


                public void onClick(DialogInterface dialog, int which) {
                    Log.v("touched: ", items[which].toString());

                }} 
                );


            return builder.create();

        }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

    }

}

And the code that launches the dialog:

public OnClickListener listener = new OnClickListener() {
    public void onClick(View v) {
        showNationalityDialog();
    }
};

private void showNationalityDialog() {
    FragmentManager fm = getSupportFragmentManager();
    ChooseDialog nationalityDialog = new ChooseDialog("Nationality");

    nationalityDialog.show(fm, "fragment_edit_name");
}

Best Answer

I know this question never drew much attention, but I finally solved the problem.

By using the listview that I created in XML rather than setting the builder's adapter, I managed to get rid of all the excess space.

Here's what the new code looks like:

    switch (editText.getId()) {
    case (0) :
    ListView list = (ListView) dialogLayout.findViewById(R.id.listView1);
    list.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist, 
            activity.getResources().getStringArray(R.array.ageArray)));
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            editText.setText(activity.getResources().getStringArray(R.array.ageArray)[arg2]);
            dismiss();
        }   
    });
    builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(activity) : 
        new AlertDialog.Builder(activity, android.R.style.Theme_Translucent);
    builder.setNegativeButton("Cancel", this);
    builder.setView(dialogLayout);

    return builder.create();
Related Topic