I am trying to make a simple alert dialog box for the user to enter the username and password and send it back to the app and close the dialog. I am at the stage where I am trying to just get the text entered into the edit text boxes as Strings, but when I try to add them to a string, they are empty. But, the toast displays what was typed into the box, like in the example below: Why are the pwtest and untest strings empty?
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
public class createid extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.create, null);
AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
builder.setTitle("Create ID");
builder.setView(view);
final EditText userid = view.findViewById(R.id.newusername);
final EditText password = view.findViewById(R.id.newpassword);
final String pwtest = password.getText().toString();
final String untest = userid.getText().toString();
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Create ID", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "User ID:" +userid.getText().toString() + "Password:" + password.getText().toString() +
"userstring is:" + untest + "password string is:" + pwtest, Toast.LENGTH_LONG).show();
}
});
return builder.create();
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
Toast.makeText(getActivity(), "Dialog Cancelled", Toast.LENGTH_SHORT).show();
}
}
You're calling the code at the wrong time, the problem is you're asking for the value in the edit text before the user could do anything with the edit text, thus you're recieving empty string
That code should be added in onClick interface instead of being with initialized
Correct code is:
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.create, null);
AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
builder.setTitle("Create ID");
builder.setView(view);
final EditText userid = view.findViewById(R.id.newusername);
final EditText password = view.findViewById(R.id.newpassword);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Create ID", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// You should get the String value here, after they're done writing in edit text.
final String pwtest = password.getText().toString();
final String untest = userid.getText().toString();
Toast.makeText(getActivity(), "User ID:" +userid.getText().toString() + "Password:" + password.getText().toString() +
"userstring is:" + untest + "password string is:" + pwtest, Toast.LENGTH_LONG).show();
}
});
return builder.create();
}