I have created a DialogFragment and I would like to add an EditText but when I try and add it like this:
final EditText input = new EditText(this);
I get an error on the "this" saying "The constructor EditText(EncryptionDialogFragment) is undefined".
My ultimate goal is to have the user enter their password this way.
public class EncryptionDialogFragment extends DialogFragment {
final EditText input = new EditText(this);
static EncryptionDialogFragment newInstance(String title){
EncryptionDialogFragment fragment = new EncryptionDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle("Enter Password:")
.setView(input)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((MainActivity)getActivity()).doPositiveClick();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((MainActivity)getActivity()).doNegativeClick();
}
}).create();
}
}
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
This method is deprecated. androidx.
Use getActivity()
final EditText input = new EditText(getActivity());
and dont use it in field, initialize it in onCreateView where getActivity will not return null
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