Reputation: 307
My question is how to get data that users input into my EditText
then show the data by using Toast.makeTest
method.
I don't know how to access to the EditText
in my fragment
and this link didn't worked for me:
getText from a EditText in a DialogFragment
This is my code:
public class FragmentDialog extends DialogFragment {
EditText entry;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.alert_dialog).setTitle("Add your text").setMessage("HElllloooooooo").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
Upvotes: 0
Views: 2037
Reputation: 535
How Edit Text Works:
First you should bind it:
entry = view.findViewById(R.id.MyEdtText);
Second To access the written data(text) in the edit you should call:
entry.getText().toString()
To Show the written text in Toast you have to:
if(TextUtils.isEmpty(entry.getText().toString()){
Toast.makeText(getActivity(), "No Text Entered", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity(), "The Text Entered Is"+ entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
The specific solution for your case is:
public class FragmentDialog extends DialogFragment {
private EditText entry;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View DialogView = inflater.inflate(R.layout.alert_dialog,null);
entry = DialogView.findViewById(R.id.entryEditText); // replace it with the correct XML ID
builder.setView(DialogView).setTitle("Add your text").setMessage("HElllloooooooo").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), "canceled operation", Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("ADD", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), "the edit text value: "+entry.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
And to show the dialog , call in your activity the following code:
FragmentDialog dialog = new FragmentDialog();
dialog.show(getSupportFragmentManager(), "YourDialog");
Proof of concept(See how it works):
Upvotes: 2
Reputation: 248
You have to findViewbyId on onCreateView like below
public class FragmentDialog extends DialogFragment {
EditText entry;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_username, container);
entry = (EditText) view.findViewById(R.id.username);
mEditText.requestFocus();
return view;
}}
Upvotes: 0