Set maximum height for Alert Dialog

3

My dialog has a list, and when this list has many entries, the dialog cuts the buttons in the middle, as in the image:

HowdoIsetamaximumheightfornotbeingcutlikethis?

DialogCode

publicvoiddeleteCategoria(intdespesa){mAba=despesa;finalCategoriaDAOdao=newCategoriaDAO(this);finalList<Categoria>list=dao.getLista(despesa);ArrayAdapter<Categoria>aa=newArrayAdapter<>(this,android.R.layout.select_dialog_multichoice,list);finalListViewlv=newListView(this);lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);lv.setAdapter(aa);AlertDialog.Builderab=newAlertDialog.Builder(this).setTitle("Apagar categorias");
    ab.setView(lv);
    ab.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            return;
        }
    });
    ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            int len = lv.getCount();
            SparseBooleanArray checked = lv.getCheckedItemPositions();

                for (int y = 0; y < len; y++) {
                    if (checked.get(y)) {
                        CategoriaDAO catDAO = new CategoriaDAO(getApplicationContext());
                        Categoria item = list.get(y);
                        catDAO.deletar(item);
                    }else if(checked.size() == 0){
                        Toast.makeText(getApplicationContext(), "Selecione uma categoria", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            Intent intente = new Intent(getApplicationContext(), CategoriaListActivity.class);
            intente.putExtra("aba", mAba);
            intente.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_NO_ANIMATION);
            getApplicationContext().startActivity(intente);
        }
    });
    ab.show();
}
    
asked by anonymous 16.06.2015 / 18:41

1 answer

1

As I said in the comment, do not create the ListView in the JAVA code, but rather in an XML layout.

Create the layout in the same way as if it were to be used in Activity .

dialog_list.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

In the code inflate it and assign it to AlertDialog using the AlertDialog.Builder.setView() method:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_list, null);

//Obtém a referência a ListView
ListView lv = (ListView)view.findViewById(R.id.list);

lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(aa);

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Apagar categorias")
  .setView(view);
....
....
    
16.06.2015 / 19:38