androidandroid-studioapplication-design

Android Studio - getting input from EditText View


Inside of my enteredName method, I want to be able to check to make sure the user has entered their name inside of the name text box. I know that I already made the input equal to name by using findViewById. What I'm having trouble with is how do you get that input so you can use in the enteredName method as well? Do I need to use findViewById each time I'm referring to what the user entered into the box?

    public class MainActivity extends AppCompatActivity {

        EditText name;

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

            name = findViewById(R.id.name);    

        }

        public boolean enteredName(){
}

Solution

  • Your first question.

    how do you get that input so you can use in the enteredName method as well?

    We can get their input by using this one.

    name.getText().toString();
    

    So you want to use in method boolean enteredName(). You can do something like this.

    public boolean enteredName(){
       if (name.getText().toString().isEmpty()) return false;
      return true;
    }
    

    your 2nd qestion.

    Do I need to use findViewById each time I'm referring to what the user entered into the box?

    Yes. it really necessary to link using findViewById. If you don't want to use it, you can use butterKnife lib.