I mimicked what I thought was fairly standard Dialog
code:
public class DChooseSeparator extends DialogFragment
{
// ...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder
.setTitle("My Title")
.setView(myDialogLayout)
.setPositiveButton(getString(R.string.sOKButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(myEditText.getText().toString().equals("")) // disable positive button if this is empty
{
Toast.makeText(getActivity(), "enter something!", Toast.LENGTH_SHORT).show();
}
else { myListener.onSet(myEditText.getText().toString()); }
}
})
.setNegativeButton(getString(R.string.sCancelButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
return builder.create();
}
}
And in onStart
of the Fragment
that shows it:
sepButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog();
myDialog.show(getFragmentManager(), "tMyDialogTag");
myDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false); // DOES NOT WORK
}
}
However, this does not work, as the getButton
function is not available for my DialogFragment
. I can also not do this in the DialogFragment
class, as I need to show()
it first.
So...where exactly can/should I disable the Button
? Do I really have to move the whole creation of the Dialog
to the onClick
method?
Thank you in advance for your help.
You can enable or disable the Button
after the view of the FragmentDialog
has been created. So you have to call it in the onStart()
method of your Dialog.
See my code:
public class DChooseSeparator extends DialogFragment
{
// MEMBER
private AlertDialog dialog;
private static boolean mEnableButton;
// You need an empty constructor: "All subclasses of Fragment must include a public empty constructor. "
// like it's described in the Fragment API -> so create a new Insatnce with this static methjod
public static DChooseSeparator newInstance(boolean enableButton){
mEnableButton = enableButton;
return new DChooseSeparator();
}
// ...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder
.setTitle("My Title")
.setView(myDialogLayout)
.setPositiveButton(getString(R.string.sOKButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(myEditText.getText().toString().equals("")) // disable positive button if this is empty
{
Toast.makeText(getActivity(), "enter something!", Toast.LENGTH_SHORT).show();
}
else { myListener.onSet(myEditText.getText().toString()); }
}
})
.setNegativeButton(getString(R.string.sCancelButton), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
dialog = builder.create()
return dialog;
}
@Override
public void onStart(){
super.onStart();
dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(mEnableButton);
}
}
Now you can call your Dialog like this:
sepButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialog myDialog = new MyDialog(false);
myDialog.show(getFragmentManager(), "tMyDialogTag");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With