[ How to take text input with DialogFragment in Android? ]
here in this question same as mine we bring out the text from the editText
in the MainActivity
.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
listener = (AddcustomerListener) getActivity();
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.add_customer_dialog, null);
builder.setView(view)
.setPositiveButton("save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/////////////here////////////
EditText user = view.findViewById(R.id.username);
Log.i( "sad",user.getText().toString());
//listener.onDialogAddCustomer(AddCustomerDialog.this);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
return builder.create();
}
I used the approach we use in a ViewHolder
of a RecyclerView
adapter to access the elements .
I had multiple editTexts
and was trying to work around , my code works but is there any harm in using the builder's view this way to access the edit text .
Still learning :)