DialogFragment with extra space below ListView

As you can see, below the bottom list item in my ListView there is excess space that I cannot get rid of. I tried Relative and Linearlayout, both look like this. Here is 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"); } 
+4
source share
2 answers

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

Using the list I created in XML, instead of installing the builder adapter, I managed to get rid of all the extra 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(); 
+3
source

If you are customizing the view in the alert dialog (via setView ()), ONLY has a ListView, you do not need to use a custom view. The builder will automatically add the ListView to the view if the adapter is called. The extra space at the end of the list probably looks like your custom view with no content.

For instance:

  @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final AccountChooserListAdapter adapter = new AccountChooserListAdapter(getActivity(), R.layout.choose_account_list_item, accountMetadataFactory.getAccountsAsList()); return new AlertDialog.Builder(getActivity()) .setCancelable(true) .setTitle(getActivity().getString(R.string.title_add_account)) .setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { listener.onAddAccount(which); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .create(); } 
+2
source

Source: https://habr.com/ru/post/1414464/


All Articles