Search code examples
android-alertdialog

AlertDialogs from a public class


I am going through an Android application that I wrote in 2014 and am replacing Dialogs with AlertDialogs

Previously, I had been using the following Dialog code to created a popup (from this popup) that would appear when "Purge Table" was selected, and would warn users of the results of their actions.

public class locationPopupWindow {
Context ctx;
Button btnDismiss, btnSaveRecord, btnPurgeTable, btnUpdateRecord, btnDeleteRecord, btnClearForm, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;

public locationPopupWindow(Context ctx){
    this.ctx = ctx;
}
public void init(){
    LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popUpView = inflater.inflate(R.layout.location_popup_layout, null, false);
    final PopupWindow popup = new PopupWindow(popUpView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    popup.setContentView(popUpView);

...     
    
    btnPurgeTable=(Button)popUpView.findViewById(R.id.btnPurgeTablexml);
    btnPurgeTable.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            vibrate(100);
            openRedDialog();
        }
    });
}

private void openRedDialog() { 
  final Dialog redDialog = new Dialog(this.ctx,android.R.style.Theme_Translucent);
  redDialog.setCancelable(true);
  redDialog.setContentView(R.layout.red_dialog);

  TextView tvRedDialogBody = (TextView)redDialog.findViewById(R.id.tvRedDialogtitle);
  tvRedDialogBody.setTypeface(null, Typeface.BOLD);
  tvRedDialogBody.setText(" Are you really sure you want to permanently delete your data???");

  Button btnDeleteTable = (Button) redDialog.findViewById(R.id.btndeletetable);
  Button btnCancel = (Button)redDialog.findViewById(R.id.btncanceldelete);
  btnDeleteTable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            vibrate(100);
            purgeTable();
            redDialog.dismiss();
            Toast.makeText(ctx.getApplicationContext(), "Lookup table was purged. Please add new values.", Toast.LENGTH_LONG).show();
        }
    });
    btnCancel.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            vibrate(100);
            redDialog.dismiss();
        }
    });
    new CountDownTimer(5000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
        }
        @Override
        public void onFinish() {
            redDialog.dismiss();
        }
    }.start();
    redDialog.show();
   }
...
}

I have swapped in the following AlertDialog implementation of openRedDialog(),

   private void openRedDialog(Context context) {//uses context
    AlertDialog openRedDialog = new AlertDialog.Builder(context).create(); 
    openRedDialog.setTitle("Warning");
    openRedDialog.setMessage("This will delete the whole table");
    openRedDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    openRedDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Delete Table",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    purgeTable();
                    dialog.dismiss();
                }
            });
    openRedDialog.show();
}

which works fine.

Question: How can I inflate my .xml resources when I implement AlertDialog as above? Thanks in advance...


Solution

  • Adding this,

    LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.red_dialog, null);
    

    to get this,

       private void openRedDialog(Context context) {
        AlertDialog openRedDialog = new AlertDialog.Builder(context).create();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.red_dialog, null);
    

    included the xml in the view.