EditText getText () returns an empty string

I have a button action, when the user clicks the button, an AlertDialog appears with 2 EditText, where you enter the email address and password to enter. When I try to get text from EditText, I always get only empty lines. The login_alert layout is the AlertDialog layout. Here is the code:

View view = getLayoutInflater().inflate(R.layout.login_alert, null, false); String email = ((EditText) view.findViewById(R.id.emailEditText)).getText().toString(); String password = ((EditText) view.findViewById(R.id.passwordEditText)).getText().toString(); System.out.println("DEBUG: "+email+", "+password); // Empty strings 

EDIT: Activity Code:

  public class MainActivity extends FragmentActivity { public static final String mAPP_ID = "..."; public static final String USER_DB_URL = "..."; AssetsExtracter mTask; private MainFragment mainFragment; private List<User> usersList = new ArrayList<User>(); private User currentUser = null; private Button labLoginButton; private EditText emailET; private EditText passwordET; private ProgressDialog dialog; private View alertView; /* THIS IS THE SOLUTION */ boolean userIsLogged = false; static { IMetaioSDKAndroid.loadNativeLibs(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(R.layout.activity_main); /*View view = getLayoutInflater().inflate(R.layout.login_alert, null, false); BEFORE*/ alertView = getLayoutInflater().inflate(R.layout.login_alert, null, false); emailET = (EditText) view.findViewById(R.id.emailEditText); passwordET = (EditText) view.findViewById(R.id.passwordEditText); labLoginButton = (Button) findViewById(R.id.loginLabButton); updateLoginButton(); dialog = new ProgressDialog(this); dialog.setMessage("Signin in..."); if (savedInstanceState == null) { // Add the fragment on initial activity setup mainFragment = new MainFragment(); getSupportFragmentManager().beginTransaction() .add(android.R.id.content, mainFragment).commit(); } else { // Or set the fragment from restored state info mainFragment = (MainFragment) getSupportFragmentManager() .findFragmentById(android.R.id.content); } mTask = new AssetsExtracter(); mTask.execute(0); } /* THIS METHOD IS CALLED BY THE LOGIN BUTTON IN THE MAIN ACTIVITY LAYOUT */ public void onLabLoginButtonClick(View v) { if (userIsLogged) { currentUser = null; userIsLogged = false; updateLoginButton(); Toast.makeText(this, "Disconnected from Lab", Toast.LENGTH_SHORT) .show(); } else { /*View messageView = getLayoutInflater().inflate( R.layout.login_alert, null, false); BEFORE */ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.icon_launcher); builder.setTitle(R.string.login_string); builder.setView(alertView); /* USING THE GLOBAL VARIABLE */ builder.setPositiveButton("Sign me", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface d, int which) { dialog.show(); // Download user and return a List of User DownloadFilesAsyncTask task = new DownloadFilesAsyncTask(USER_DB_URL) { @Override protected void onPostExecute(final List<User> result) { usersList = result; loginCheckRoutine(); //HERE I MANAGE THE LOGIN AND GETTING EMPTY STRING } }; task.execute(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.create(); builder.show(); } } public void updateLoginButton() { if (userIsLogged) { labLoginButton.setText(R.string.logout_string); } else { labLoginButton.setText(R.string.login_string); } } public void loginCheckRoutine() { String email = emailET.getText().toString(); String password = passwordET.getText().toString(); System.out.println("DEBUG: " + email + ", " + password); // EMPTY // controllo nella lista se c'รจ l'utente coi dati inseriti for (int i = 0; i < usersList.size(); i++) { if (usersList.get(i).getEmail().equals(email) && password.equals("admin")) { currentUser = usersList.get(i); userIsLogged = true; updateLoginButton(); dialog.dismiss(); break; } } if (!userIsLogged) { userIsLogged = false; updateLoginButton(); dialog.dismiss(); Toast.makeText(MainActivity.this, "Login Failed", Toast.LENGTH_SHORT).show(); } } } 

SOLVING THE PROBLEM, SOLUTION: In onCreate (), I inflate the alert_dialog layout in the View variable. I made a variable of the form View global (before onCreate ()) and then in onLabLoginButtonClick () I do not inflate the view again, but I use this global instance in onCreate (). hope this is clear. Thank you all!

+7
android string android-edittext
source share
7 answers

You getText right after initialization. As long as you don't have text in xml, you won't get text. In the onclick of the alertdialog button get the text.

Announces

 EdiText ed1,ed2; // before onCreate if in activity and onCraeteView in fragment 

as an instance variable

 View view = getLayoutInflater().inflate(R.layout.login_alert, null, false); ed1= (EditText) view.findViewById(R.id.emailEditText)) ed2 = (EditText) view.findViewById(R.id.emailEditText); 

then in the Warning dialog box, click

  String email = ed1.getText().toString(); String password= ed2.getText().toString() 
+4
source share

you should get the text when you press the login button of the warning dialog

the above code you get the text when you show the warning dialog, it always returns an empty string, you must follow the following procedure first you create a custom layout of the notification window containing two editing texts and one button the user writes the text to edittext to enter and gives a password and then press the login button when you call the login button to enter the system, you can easily get the text edittext

+4
source share

You are trying to get text immediately after inflating the view. Try this when the user clicks the Finish button.

+1
source share

Before onCreate add:

 EditText email; EditText pass; 

Add this to your onCreate

 etEmail (EditText) view.findViewById(R.id.emailEditText); etPass (EditText) view.findViewById(R.id.emailEditText); 

Then add this when the button is pressed

 String email = etEmail.getText().toString(); String pass = etEmail.getText().toString(); 
+1
source share

Alternatively, add a TextChangedListener to the text view to change the line change every time the text text changes. A textwatcher is also possible

0
source share

You should get the text when you click the "Save" or "Finish" button. If you get this text when you click the warning button, you may end up taking it several times.

0
source share

Just make sure the editText.getText.toString () method is inside the OnClick () method, for example:

 TextView submit = enquiryFragment.findViewById(R.id.query_submit_button); submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ query_type = query_type_editText.getText().toString(); query_text = query_editText.getText().toString(); if (query_text.length()!=0 && query_type.length()!=0) { postQuery(query_type, query_text, store_id); // Log.e("query_type ",query_type ); }else{ Toast.makeText(getContext(), "Enter something !", Toast.LENGTH_SHORT).show(); } } }); 
0
source share

All Articles