Thursday 28 July 2011

Alert Dialog

I found people facing problem with AlertDialog as:
1. AlertDialog disappears when device rotates from portrait to landscape or vice-versa.
2. The developer does not want AlertDialog to disappear until the condition is satisfied when button clicked

To get these things done, you need to override the method:

@Override
protected Dialog onCreateDialog(int id) { 
    LayoutInflater inflator = LayoutInflater.from(context);
    View view = inflator.inflate(R.layout.yourview, null);
    Button positive = (Button)view.findViewById(R.id.btn_positive);
    Button negative = (Button)view.findViewById(R.id.btn_negative);
   
    positive.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
            removeDialog(0);
        }
    });
   
    negative.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
            removeDialog(0);
        }
    });
   
    AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.setView(view);
   
    return dialog;
}


No comments:

Post a Comment