0

I am at a loss as to how to reference an item in a custom dialog. I have looked here, here, here, and other places. I cannot get the checkbox in the custom dialog to be referenced in an onclick event, or when I try to call it directly in the positive buttons onClick.

XML:

<TextView
    android:id="@+id/dialog_title"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:layout_weight="1"
    android:text="@string/delete_db_on_exit"
    android:textColor="@color/Black" />

<TextView
    android:id="@+id/dialog_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:text="@string/confirm_delete"
    android:textColor="@color/Black" />

<CheckBox
    android:id="@+id/confirm_delete_checkbox"
    android:paddingTop="5dp"
    android:background="@color/White"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/confirm_delete_checkbox"/>

Code in Activity that calls dialog:

private LayoutInflater inflator;
private View dialogView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_administration);
    inflator = this.getLayoutInflater();
    dialogView = inflator.inflate(R.layout.custom_dialog, null);
}

public void deleteDatabases(View view){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this)
            .setTitle(R.string.delete_db_on_exit)
            .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null))
            .setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    if (((CheckBox) dialogView.findViewById(R.id.confirm_delete_checkbox)).isChecked()) { //Never true. No exceptions thrown
                        setDeleteDatabaseOnExit();
                        dialog.cancel();
                        //Navigate back to main activity
                        Intent mainActivityIntent = new Intent(AdministrationActivity.this, MainActivity.class);
                        startActivityForResult(mainActivityIntent, 1);
                    }
                }
            });

    CheckBox confirmDeleteCheckbox = (CheckBox)dialogView.findViewById(R.id.confirm_delete_checkbox);
    confirmDeleteCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int x = 0;
            x = 1;
        }
    });

    AlertDialog dialog = dialogBuilder.create();
    dialog.show();
}
Community
  • 1
  • 1
Soatl
  • 10,224
  • 28
  • 95
  • 153

1 Answers1

0

Change:

        .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null))

to:

        .setCustomTitle(dialogView)

In your dialog you are creating a new custom_dialog view which also has a checkbox but is not same as the one in dialogView. That's why when you check it true the one in dialog set's it self to true but the one in dialogView is still false.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • That seemed to fix the issue! Thanks for that! I have been trying to figure this out for hours – Soatl Jul 21 '14 at 17:26