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();
}