2

This is my code: here some edit texts are there.

public class MainActivity extends ActionBarActivity {

@InjectView(R.id.edt_fname) protected EditText account_fname;
@InjectView(R.id.edt_lnames) protected  EditText account_lname;
@InjectView(R.id.edt_userid) protected EditText account_userid;
@InjectView(R.id.edt_pwd) protected EditText account_password;
@InjectView(R.id.edt_reenter) protected EditText account_reenter_pswd;
@InjectView(R.id.nxt_btn1) protected Button next_acct;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);        
    next_acct.setEnabled(false);
    if(( !account_fname.getText().toString().equals("")) &&
            ( !account_lname.getText().toString().equals("")) &&
            ( !account_userid.getText().toString().equals("")) &&
            ( !account_password.getText().toString().equals("")) &&
            ( !account_reenter_pswd.getText().toString().equals("")) )
    {
        next_acct.setEnabled(true);
        next_acct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), PersonalInfo.class));
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });
    }   }

My aim is that when the fields are empty, then the submit button is disabled.
If all fields are filled with some text, then the submit button is enabled.

How can I do this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

4 Answers4

1

this is very simple...

if(edttext.getText().toString().equals(edttext2.getText().toString()))
    {
        submit_button.setEnabled(true);

    }
    else
    {
        submit_button.setEnabled(true);
    }
  • 1
    first check your conditions and then write code for button active or inactive modes like above. –  Apr 01 '15 at 11:06
0

try this

if((account_fname.getText() != null ) &&
        ( account_lname.getText() != null) &&
        ( account_userid.getText() != null) &&
        ( account_password.getText() != null) &&
        ( account_reenter_pswd.getText() != null) )
{
    next_acct.setEnabled(true);
}
likith sai
  • 527
  • 1
  • 6
  • 21
0

You're doing it inside the onCreate method. You need to add a textChangeListener on the EditText then place your if statement in the textChangeListener or create a method then put the if statement in the method then call the method in the TextChangeListener.

For reference on TextChangeListener: Counting Chars in EditText Changed Listener

Community
  • 1
  • 1
Hiroga Katageri
  • 1,345
  • 3
  • 21
  • 40
0

The challenge here is really in gathering the content of the EditText fields. That is, once you have a simple call to retrieve all interesting TextView or EditText fields, then the problem becomes trivial.

I tested this, seems to work. I believe it's less fragile to changes in the structure or nesting of the EditText fields of the layout.

To answer the question in the topic, the submit button can be enabled if the array contains no "null" entries from the result of the "gatherEditTextContent(View root)" method.

public static String[] gatherEditTextContent(View root) {
    final Vector<String> v = new Vector();
    Boolean stop = false;
    scanEditText(root, new ETCallback() {
        @Override
        public boolean onEditText(EditText editText) {
            v.add((editText.getText() == null ? "null" : editText.getText().toString()));
            return false;
        }
    });
    return v.toArray(new String[v.size()]);
}

/** initiate by passing v= root, callback non-null */
public static boolean scanEditText(View v, ETCallback callback) {

    boolean stop = false;
    if (v instanceof EditText) {
        if (callback != null) stop = callback.onEditText((EditText)v);
    }
    else if (v instanceof ViewGroup) {
        ViewGroup vg = (ViewGroup) v;
        for (int i=0; (!stop && i < vg.getChildCount()); i++) {
            View child = vg.getChildAt(i);
            stop = scanEditText(child, callback);
        }
    }
    return stop;
}

private interface ETCallback {
    public boolean onEditText(EditText editText);
}
Craig D
  • 351
  • 2
  • 7