Set maximum height for Alert Dialog

Asked

Viewed 323 times

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 :

Dialog

How do I set a maximum height not to get cut this way?

Dialog code

public void deleteCategoria(int despesa){
    mAba = despesa;
    final CategoriaDAO dao = new CategoriaDAO(this);
    final List<Categoria> list = dao.getLista(despesa);
    ArrayAdapter<Categoria> aa = new ArrayAdapter<>(this,android.R.layout.select_dialog_multichoice,list);
    final ListView lv = new ListView(this);
    lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    lv.setAdapter(aa);
    AlertDialog.Builder ab = new AlertDialog.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();
}
  • Do not create the Listview in the JAVA code, but in a layout in XML

  • Create a normal layout like Activity xml? how do I put it in the dialog? @ramaral

  • Create as if it were one Activity and use ab.setContentView(R.layout.o_seu_layout);.

  • I have no way to set a Content view, only setView @ramaral

  • I didn’t realize I was using the Builder, I’ll post an answer.

1 answer

1


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

Create the layout in the same way as if it were to be used in a 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 do the inflate and attribute it to Alertdialog using the method AlertDialog.Builder.setView():

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);
....
....
  • It worked perfectly @ramaral, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.