Reputation: 557
I was wondering how I could get the input from an editText box in a popupwindow. I tried 2 ways. One with a layout inflator and one without but I either get "" or null even though i typed something into the box
This one returns the "" :
// get the username and password inputs
View inflatedView = getLayoutInflater().inflate(R.layout.login_popup, null);
EditText usernameInput = (EditText) inflatedView.findViewById(R.id.username_login_input);
EditText passwordInput = (EditText) inflatedView.findViewById(R.id.password_login_input);
final String usernameString = usernameInput.getText().toString();
final String passwordString = passwordInput.getText().toString();
This one returns null :
// get the username and password inputs
EditText usernameInput = (EditText) findViewById(R.id.username_login_input);
EditText passwordInput = (EditText) findViewById(R.id.password_login_input);
final String usernameString = usernameInput.getText().toString();
final String passwordString = passwordInput.getText().toString();
I am trying to get it from login_popup.xml which is not generated from an activity
This is where the code is from
EDIT
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.login_popup, null);
ppw = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
ViewGroup parentLayout = (ViewGroup) findViewById(R.id.title_page_layout);
// set the position and size of popup
ppw.showAtLocation(parentLayout, Gravity.CENTER, 10, 20);
Upvotes: 0
Views: 3384
Reputation: 38595
Try the following instead when your popup window is closed:
View contentView = ppw.getContentView();
EditText usernameInput = (EditText) contentView.findViewById(R.id.username_login_input);
EditText passwordInput = (EditText) contentView.findViewById(R.id.password_login_input);
final String usernameString = usernameInput.getText().toString();
final String passwordString = passwordInput.getText().toString();
I think your current approach is inflating new views, rather than obtaining the existing views from the popup window.
Upvotes: 5