0

I am trying to learn android in java but I m stuck. Here is my method to get values from EditText fields.

public void sendFeedback(View button) { 


      System.out.println("Do not fail");
           final EditText nameField = (EditText) findViewById(R.id.Name);
           String name = nameField.getText().toString();
           final EditText numberField = (EditText) findViewById(R.id.Cost);
           String test=numberField.getText().toString();
           Integer x = Integer.parseInt(test.trim());
           System.out.println(x);
           System.out.println(name);
         }

This is the xml for button.

<Button
    android:id="@+id/ButtonSendFeedback"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:onClick="sendFeedback"
    android:layout_width="fill_parent">
</Button>

What I want to do is following. If someone leaves EditText Null or enters a non-integer value to numberfield I want to DISPLAY that in android but System.out.println() does not display anything. If the previous case does not happen I want to save the variables to a local database. How can I do that?

4 Answers4

1

Try something like this:

public void sendFeedback(View button) {
    Toast.makeText(this, "Do not fail", Toast.LENGTH_SHORT).show();
    final EditText nameField = (EditText) findViewById(R.id.Name);
    if(nameField != null) {
        String name = nameField.getText().toString();

        if (!name.isEmpty()) {
            Toast.makeText(this, "Name: " + name, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Namefield is empty!" , Toast.LENGTH_SHORT).show();
        }
    }
    final EditText numberField = (EditText) findViewById(R.id.Cost);
    if(numberField != null) {
        String test = numberField.getText().toString();
        if(!test.isEmpty()) {
            int x = Integer.parseInt(test.trim());
            Toast.makeText(this, "Cost: " + x, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "You haven't entered a number", Toast.LENGTH_SHORT).show();
        }
    }
}
babadaba
  • 814
  • 5
  • 20
  • This crashes the program. Probably because of trying to use a null value. But it works correctly when I enter values to inputs. What I want is to notify user when they try to submit while form is empty – Felipe Hield Jul 01 '16 at 20:30
  • I edited the code in my answer, you should get notified if no text is entered in either field. – babadaba Jul 01 '16 at 20:39
0

If you want to show that message to the user you can use Toast or Snackbar

https://developer.android.com/guide/topics/ui/notifiers/toasts.html

https://developer.android.com/reference/android/support/design/widget/Snackbar.html

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
0

System.out.println logs the message to the console. So, you cannot use it to display error in the UI. To show error in Android you should use these :

Community
  • 1
  • 1
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
0

System.out.println() prints to the console. Try using Toast to notify the user invalid input