Reputation: 1034
I have the following code.It shows a dialog with a textview and some buttons.When a button is pressed,the dialog should close.But it doesn't.Is it bugged ? I tried both dialog.dismiss and dialog.cancel but it just won't work.
What's the solution ?
AlertDialog.Builder alert = new AlertDialog.Builder(
Gestionarez.this);
final TextView Dtv = new TextView(Gestionarez.this);
printeaza=new Button(Gestionarez.this);
stergere=new Button(Gestionarez.this);
trimitere=new Button(Gestionarez.this);
final AlertDialog dialog = alert.create();
trimitere.setText("Trimite");
trimitere.setTextSize(10);
trimitere.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
printeaza.setText("Printeaza");
printeaza.setTextSize(10);
printeaza.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
dialog.cancel();
}
});
stergere.setText("Sterge");
stergere.setTextSize(10);
stergere.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean deleted = file.delete();
Toast.makeText(Gestionarez.this, str+ " a fost sters ",
Toast.LENGTH_SHORT).show();
lv2.invalidateViews();
}
});
LinearLayout ldialog = new LinearLayout(Gestionarez.this);
LinearLayout ldialogb = new LinearLayout(Gestionarez.this);
ldialog.setOrientation(LinearLayout.VERTICAL);
ldialogb.setOrientation(LinearLayout.HORIZONTAL);
Dtv.setText(text);
ldialog.addView(Dtv);
ldialogb.addView(trimitere);
ldialogb.addView(printeaza);
ldialogb.addView(stergere);
ldialog.addView(ldialogb);
ldialogb.setGravity(Gravity.CENTER | Gravity.BOTTOM);
Dtv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
Dtv.setPadding(60, 60, 60, 60);
alert.setView(ldialog);
alert.show();
}
});
}
Upvotes: 0
Views: 1952
Reputation: 131
try dialog.finsh();
then dialog.dismiss();
or this onClick :
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
Upvotes: 0
Reputation: 13705
Seems like you are showing, a new created dialog which reference you dont hold in
alert.setView(ldialog);
alert.show();
And you are trying to dismiss a dialog which reference is different from that one:
dialog.dismiss();
dialog.cancel();
Remove the final from the dialog and change the last line "alert.show();
" for
dialog = alert.create();
dialog.show();
And now you have a reference of that dialog, and it will work....
Upvotes: 2