Android - Can't get value from EditText inside Custom Dialog
12,267
Solution 1
I was trying to do the same thing and i get the same error. I don't no why. I already use AlertDialog.Builder in the past and get no trouble. But in your case change this code:
public void onClick(DialogInterface dialog,
int id) {
//This is the input I can't get text from
EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
//query is of the String type
query = inputTemp.getText().toString();
newQuery();
getJSON newData = new getJSON();
newData.execute("Test");
}
By this one:
public void onClick(DialogInterface dialog,
int id) {
Dialog f = (Dialog) dialog;
//This is the input I can't get text from
EditText inputTemp = (EditText) f.findViewById(R.id.search_input_text);
query = inputTemp.getText().toString();
...
}
This solution works for me and it seems to be the same for you. Found on stackoverflow
Solution 2
Use this instead :
View myLayout = nflater.inflate(R.layout.city_dialog, null);
EditText myEditText = (EditText) myLayout.findViewById(R.id.myEditText);
String valueOfEditText = myEditText.getText().toString();
Comments
-
Alec Moore almost 2 years
I have been unsuccessful in getting the input from my EditText object inside my custom dialog.
public class SetCityDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater factory = LayoutInflater.from(MainActivity.this); final View view = factory.inflate(R.layout.city_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.city_dialog, null)) // Add action buttons .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { //This is the input I can't get text from EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text); //query is of the String type query = inputTemp.getText().toString(); newQuery(); getJSON newData = new getJSON(); newData.execute("Test"); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { SetCityDialog.this.getDialog().cancel(); } }); return builder.create(); } }
I don't get any exceptions, but the variable query is set to an empty string. Any help would be fantastic.