0

My goal is to get an input at the edit-text fields. The idea was to disable the 'submit' button as long as the user didn't enter all the details. However I got a problem while trying to do so.

I do not have problem with the Intent or the screens.

The code:

public class Welcome extends Activity  
{

EditText efn,eln,eage;
Button submit;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);




    efn=(EditText)findViewById(R.id.fname);
    eln=(EditText)findViewById(R.id.lname);
    eage=(EditText)findViewById(R.id.age);
    submit=(Button)findViewById(R.id.submit);


    submit.setEnabled(CheckIfOkInput());

                                        // I guess the problem is over here <--
if(CheckIfOkInput()==false)
    {
        while(CheckIfOkInput())
        {
            efn=(EditText)findViewById(R.id.fname);
            eln=(EditText)findViewById(R.id.lname);
            eage=(EditText)findViewById(R.id.age);
        }

        submit.setEnabled(true);
    }



    submit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


                Intent iHome=new Intent (Welcome.this,Home_page.class);
                String ln=eln.getText().toString();
                String fn=efn.getText().toString();
                int age=Integer.valueOf (eage.getText().toString());

                iHome.putExtra("fname", fn);
                iHome.putExtra("lname", ln);
                iHome.putExtra("age", age);

                startActivity(iHome);



        }
    });


}

     public boolean CheckIfOkInput()
{

    if(this.eln.getText().toString()==""||this.efn.getText().toString()=="")
    {
        return false;
    }


    return true;

}

 }

And the result of this code is that the Submit button just keep being disabled forever.

Hope you guys could help me with that.

Thanks in advance,

Yaniv.

YanivGK
  • 893
  • 12
  • 18

2 Answers2

2

You can make something like the following code, on each EditText you can add a listener, and when the text of the EditText has changed, you verify if both EditText have texts. By the way, use the .equals method to compare strings, and you can use "".equals(this.eln.getText().toString()) to avoid nullpointerexceptions if this.eln.getText().toString() returns null.

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(!"".equals(this.eln.getText().toString()) && !"".equals(this.efn.getText().toString()){
            submit.setEnabled(true);
        }
    }
}
Giacomoni
  • 1,468
  • 13
  • 18
  • Ok. But what the EditTextLisnter is good for? I mean - what does it do? – YanivGK Jan 27 '14 at 18:59
  • 1
    It's a listener, the method above, onTextChanged, is called every time the text of its edittext is changed. So, every time the user write something on the edittext, it will call the onTextChanged and verify if both edittexts have some text. – Giacomoni Jan 27 '14 at 19:01
  • All right, I understood now, and the button indeed disable only when there is text. But some reason the button doesn't work after I press it. and make the app to stop working. – YanivGK Jan 27 '14 at 19:17
  • Can you post the error log? To give some action to the button, you need to use submit.setOnClickListener method, and inside the override onclick method, you can set the action of the button. – Giacomoni Jan 27 '14 at 19:28
  • Ah, I found it. I just didn't set listner on the third Edittext. – YanivGK Jan 27 '14 at 19:35
0

Don't use a while, as you will be 'doing' something all the time. you need a listener. Just like an onclick-listener for a button, but then an on-change listener for the edit-text field

Method: make a listeren, everytime (for starters, you can update this later) your change is registered, you check if the inputfield is empty. If it is, you disable the button, else you enable it.

Making a changelistener is easy to find, for instance: Counting Chars in EditText Changed Listener

The code in that question is about counting characters, but that's almost exactly what you want: count the characters, and if they are 0 (or not 0) do something.

Community
  • 1
  • 1
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • I am new with all these programing.. so I do not understand you well. Would you mind to be more specific? – YanivGK Jan 27 '14 at 18:55
  • Like - what does the EditTextListener do? – YanivGK Jan 27 '14 at 19:02
  • 1
    It "listens" for changes. That's what listeners do. An onclick listener waits (listens) for you to click, and when you do, it "fires". An onchangelistener waits until there is a change (e.g. userinput) and then fires. You don't need to be constantly checking something, you can just 'wait' (listen) for something to happen, and then check if there is a character added. – Nanne Jan 27 '14 at 19:06