Retrieving the text from an edittext












2















So I am making a little app where it takes a comma separated list and returns a random word in that list and I am having trouble getting the text from the edittext view.



I saw on stackoverflow that you're suppose to use the .getText().toString() methods in order to get the text from the edittext view. However, when I use those methods, the app stops working. I ran the debugger and my Input variable refers to an empty string and I also get an error that there is no local submit variable.



This is the java file:



public class MainActivity extends AppCompatActivity {
EditText UserInput;
Button reset;
Button Submit;
TextView result;
String Input;
String Result;
String word;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

UserInput = (EditText) findViewById(R.id.UserInput);
reset = (Button) findViewById(R.id.Reset);
Submit = (Button) findViewById(R.id.Choose);
result = (TextView) findViewById(R.id.Result);
Input = UserInput.getText().toString();
Result = "Your choice should be:";

Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random();
String List = Input.split(",");
int num = random.nextInt(List.length - 1);
word = List[num];
Result = Result + word;
result.setText(Result);
}
});
}


This is the edittext view of the activity:



<EditText
android:id="@+id/UserInput"
android:layout_width="273dp"
android:layout_height="59dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Choice A, Choice B, Choice C,..."
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Instructions" />


For example, if I write in the edittext view 'a,b'. I expect Input to be 'a,b' but it is not. When I was debugging it showed me it was an empty string and I do not know why. Also. I got another error that there is no local submit variable in the onclick method. How do I fix these errors?










share|improve this question




















  • 1





    Post logcat error.

    – Gokul Nath KP
    Jan 1 at 17:59
















2















So I am making a little app where it takes a comma separated list and returns a random word in that list and I am having trouble getting the text from the edittext view.



I saw on stackoverflow that you're suppose to use the .getText().toString() methods in order to get the text from the edittext view. However, when I use those methods, the app stops working. I ran the debugger and my Input variable refers to an empty string and I also get an error that there is no local submit variable.



This is the java file:



public class MainActivity extends AppCompatActivity {
EditText UserInput;
Button reset;
Button Submit;
TextView result;
String Input;
String Result;
String word;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

UserInput = (EditText) findViewById(R.id.UserInput);
reset = (Button) findViewById(R.id.Reset);
Submit = (Button) findViewById(R.id.Choose);
result = (TextView) findViewById(R.id.Result);
Input = UserInput.getText().toString();
Result = "Your choice should be:";

Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random();
String List = Input.split(",");
int num = random.nextInt(List.length - 1);
word = List[num];
Result = Result + word;
result.setText(Result);
}
});
}


This is the edittext view of the activity:



<EditText
android:id="@+id/UserInput"
android:layout_width="273dp"
android:layout_height="59dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Choice A, Choice B, Choice C,..."
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Instructions" />


For example, if I write in the edittext view 'a,b'. I expect Input to be 'a,b' but it is not. When I was debugging it showed me it was an empty string and I do not know why. Also. I got another error that there is no local submit variable in the onclick method. How do I fix these errors?










share|improve this question




















  • 1





    Post logcat error.

    – Gokul Nath KP
    Jan 1 at 17:59














2












2








2








So I am making a little app where it takes a comma separated list and returns a random word in that list and I am having trouble getting the text from the edittext view.



I saw on stackoverflow that you're suppose to use the .getText().toString() methods in order to get the text from the edittext view. However, when I use those methods, the app stops working. I ran the debugger and my Input variable refers to an empty string and I also get an error that there is no local submit variable.



This is the java file:



public class MainActivity extends AppCompatActivity {
EditText UserInput;
Button reset;
Button Submit;
TextView result;
String Input;
String Result;
String word;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

UserInput = (EditText) findViewById(R.id.UserInput);
reset = (Button) findViewById(R.id.Reset);
Submit = (Button) findViewById(R.id.Choose);
result = (TextView) findViewById(R.id.Result);
Input = UserInput.getText().toString();
Result = "Your choice should be:";

Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random();
String List = Input.split(",");
int num = random.nextInt(List.length - 1);
word = List[num];
Result = Result + word;
result.setText(Result);
}
});
}


This is the edittext view of the activity:



<EditText
android:id="@+id/UserInput"
android:layout_width="273dp"
android:layout_height="59dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Choice A, Choice B, Choice C,..."
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Instructions" />


For example, if I write in the edittext view 'a,b'. I expect Input to be 'a,b' but it is not. When I was debugging it showed me it was an empty string and I do not know why. Also. I got another error that there is no local submit variable in the onclick method. How do I fix these errors?










share|improve this question
















So I am making a little app where it takes a comma separated list and returns a random word in that list and I am having trouble getting the text from the edittext view.



I saw on stackoverflow that you're suppose to use the .getText().toString() methods in order to get the text from the edittext view. However, when I use those methods, the app stops working. I ran the debugger and my Input variable refers to an empty string and I also get an error that there is no local submit variable.



This is the java file:



public class MainActivity extends AppCompatActivity {
EditText UserInput;
Button reset;
Button Submit;
TextView result;
String Input;
String Result;
String word;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

UserInput = (EditText) findViewById(R.id.UserInput);
reset = (Button) findViewById(R.id.Reset);
Submit = (Button) findViewById(R.id.Choose);
result = (TextView) findViewById(R.id.Result);
Input = UserInput.getText().toString();
Result = "Your choice should be:";

Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random random = new Random();
String List = Input.split(",");
int num = random.nextInt(List.length - 1);
word = List[num];
Result = Result + word;
result.setText(Result);
}
});
}


This is the edittext view of the activity:



<EditText
android:id="@+id/UserInput"
android:layout_width="273dp"
android:layout_height="59dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Choice A, Choice B, Choice C,..."
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Instructions" />


For example, if I write in the edittext view 'a,b'. I expect Input to be 'a,b' but it is not. When I was debugging it showed me it was an empty string and I do not know why. Also. I got another error that there is no local submit variable in the onclick method. How do I fix these errors?







java android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 19:13









Fantômas

32.8k156390




32.8k156390










asked Jan 1 at 17:52









GeraltGeralt

185




185








  • 1





    Post logcat error.

    – Gokul Nath KP
    Jan 1 at 17:59














  • 1





    Post logcat error.

    – Gokul Nath KP
    Jan 1 at 17:59








1




1





Post logcat error.

– Gokul Nath KP
Jan 1 at 17:59





Post logcat error.

– Gokul Nath KP
Jan 1 at 17:59












3 Answers
3






active

oldest

votes


















2














Move Input = UserInput.getText().toString(); inside the onClick method.



Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Input = UserInput.getText().toString();
Random random = new Random();
String List = Input.split(",");
int num = random.nextInt(List.length - 1);
word = List[num];
Result = Result + word;
result.setText(Result);
}
}


You need to read the EditText only when the button is clicked, not when the app first starts.



Layman's explanation:
In your current code, you read the EditText when the app is launched. Since it is empty at the time, you get an empty string when you read it.






share|improve this answer































    3














    You are getting the text from EditText at the time of declaring because of which Input is null. Input is not watching the edittext for change in its content.



    To do this, you have to get Input again, just before submitting.



    Submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Random random = new Random();
    Input = UserInput.getText().toString();
    if (Input != null) {
    String List = Input.split(",");
    int num = random.nextInt(List.length - 1);
    word = List[num];
    Result = Result + word;
    result.setText(Result);
    }
    }
    });





    share|improve this answer
























    • Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase

      – May Rest in Peace
      Jan 1 at 18:05



















    0














    Debugger shows empty string in such case, num may become 0, which then can cause IndexOutOfBounds Exception.



    String List = Input.split(",");
    int num = random.nextInt(List.length - 1);
    word = List[num];


    Post your logcat for further assistance.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997648%2fretrieving-the-text-from-an-edittext%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Move Input = UserInput.getText().toString(); inside the onClick method.



      Submit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      Input = UserInput.getText().toString();
      Random random = new Random();
      String List = Input.split(",");
      int num = random.nextInt(List.length - 1);
      word = List[num];
      Result = Result + word;
      result.setText(Result);
      }
      }


      You need to read the EditText only when the button is clicked, not when the app first starts.



      Layman's explanation:
      In your current code, you read the EditText when the app is launched. Since it is empty at the time, you get an empty string when you read it.






      share|improve this answer




























        2














        Move Input = UserInput.getText().toString(); inside the onClick method.



        Submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Input = UserInput.getText().toString();
        Random random = new Random();
        String List = Input.split(",");
        int num = random.nextInt(List.length - 1);
        word = List[num];
        Result = Result + word;
        result.setText(Result);
        }
        }


        You need to read the EditText only when the button is clicked, not when the app first starts.



        Layman's explanation:
        In your current code, you read the EditText when the app is launched. Since it is empty at the time, you get an empty string when you read it.






        share|improve this answer


























          2












          2








          2







          Move Input = UserInput.getText().toString(); inside the onClick method.



          Submit.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
          Input = UserInput.getText().toString();
          Random random = new Random();
          String List = Input.split(",");
          int num = random.nextInt(List.length - 1);
          word = List[num];
          Result = Result + word;
          result.setText(Result);
          }
          }


          You need to read the EditText only when the button is clicked, not when the app first starts.



          Layman's explanation:
          In your current code, you read the EditText when the app is launched. Since it is empty at the time, you get an empty string when you read it.






          share|improve this answer













          Move Input = UserInput.getText().toString(); inside the onClick method.



          Submit.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
          Input = UserInput.getText().toString();
          Random random = new Random();
          String List = Input.split(",");
          int num = random.nextInt(List.length - 1);
          word = List[num];
          Result = Result + word;
          result.setText(Result);
          }
          }


          You need to read the EditText only when the button is clicked, not when the app first starts.



          Layman's explanation:
          In your current code, you read the EditText when the app is launched. Since it is empty at the time, you get an empty string when you read it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 18:04









          Susmit AgrawalSusmit Agrawal

          1,002513




          1,002513

























              3














              You are getting the text from EditText at the time of declaring because of which Input is null. Input is not watching the edittext for change in its content.



              To do this, you have to get Input again, just before submitting.



              Submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              Random random = new Random();
              Input = UserInput.getText().toString();
              if (Input != null) {
              String List = Input.split(",");
              int num = random.nextInt(List.length - 1);
              word = List[num];
              Result = Result + word;
              result.setText(Result);
              }
              }
              });





              share|improve this answer
























              • Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase

                – May Rest in Peace
                Jan 1 at 18:05
















              3














              You are getting the text from EditText at the time of declaring because of which Input is null. Input is not watching the edittext for change in its content.



              To do this, you have to get Input again, just before submitting.



              Submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              Random random = new Random();
              Input = UserInput.getText().toString();
              if (Input != null) {
              String List = Input.split(",");
              int num = random.nextInt(List.length - 1);
              word = List[num];
              Result = Result + word;
              result.setText(Result);
              }
              }
              });





              share|improve this answer
























              • Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase

                – May Rest in Peace
                Jan 1 at 18:05














              3












              3








              3







              You are getting the text from EditText at the time of declaring because of which Input is null. Input is not watching the edittext for change in its content.



              To do this, you have to get Input again, just before submitting.



              Submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              Random random = new Random();
              Input = UserInput.getText().toString();
              if (Input != null) {
              String List = Input.split(",");
              int num = random.nextInt(List.length - 1);
              word = List[num];
              Result = Result + word;
              result.setText(Result);
              }
              }
              });





              share|improve this answer













              You are getting the text from EditText at the time of declaring because of which Input is null. Input is not watching the edittext for change in its content.



              To do this, you have to get Input again, just before submitting.



              Submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              Random random = new Random();
              Input = UserInput.getText().toString();
              if (Input != null) {
              String List = Input.split(",");
              int num = random.nextInt(List.length - 1);
              word = List[num];
              Result = Result + word;
              result.setText(Result);
              }
              }
              });






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 1 at 18:03









              May Rest in PeaceMay Rest in Peace

              62911019




              62911019













              • Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase

                – May Rest in Peace
                Jan 1 at 18:05



















              • Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase

                – May Rest in Peace
                Jan 1 at 18:05

















              Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase

              – May Rest in Peace
              Jan 1 at 18:05





              Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase

              – May Rest in Peace
              Jan 1 at 18:05











              0














              Debugger shows empty string in such case, num may become 0, which then can cause IndexOutOfBounds Exception.



              String List = Input.split(",");
              int num = random.nextInt(List.length - 1);
              word = List[num];


              Post your logcat for further assistance.






              share|improve this answer




























                0














                Debugger shows empty string in such case, num may become 0, which then can cause IndexOutOfBounds Exception.



                String List = Input.split(",");
                int num = random.nextInt(List.length - 1);
                word = List[num];


                Post your logcat for further assistance.






                share|improve this answer


























                  0












                  0








                  0







                  Debugger shows empty string in such case, num may become 0, which then can cause IndexOutOfBounds Exception.



                  String List = Input.split(",");
                  int num = random.nextInt(List.length - 1);
                  word = List[num];


                  Post your logcat for further assistance.






                  share|improve this answer













                  Debugger shows empty string in such case, num may become 0, which then can cause IndexOutOfBounds Exception.



                  String List = Input.split(",");
                  int num = random.nextInt(List.length - 1);
                  word = List[num];


                  Post your logcat for further assistance.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 18:01









                  Gokul Nath KPGokul Nath KP

                  7,8851862103




                  7,8851862103






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997648%2fretrieving-the-text-from-an-edittext%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      'app-layout' is not a known element: how to share Component with different Modules

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      WPF add header to Image with URL pettitions [duplicate]