0

In my registration form if any of the mandatory field is empty,how to disable register button? After filling all the mandatory fields it has to go for the database.I am getting the error messages but not getting how to disable the Button.Here is my code,

public class MainActivity extends Activity implements View.OnClickListener{
Button log,sign;
private EditText firstname,lastname,mycity,myphone,password,cpassword;
Spinner bloodgroup,myarea;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    log = (Button) findViewById(R.id.register);
    sign = (Button) findViewById(R.id.linktologin);
    log.setOnClickListener(this);
    sign.setOnClickListener(this);
    initializeVars();
    bloodgroup = (Spinner) findViewById(R.id.bgroup) ;
    List<String> list = new ArrayList<String>();
    list.add("A+");
    list.add("A-");
    list.add("B+");
    list.add("B-");
    list.add("O+");
    list.add("O-");
    list.add("AB+");
    list.add("AB-");
    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, list);
    bloodgroup.setAdapter(adapter);

    myarea = (Spinner) findViewById(R.id.area) ;
    List<String> mylist = new ArrayList<String>();
    mylist.add("Vijaynagar");
    mylist.add("Malleshwarm");
    mylist.add("Banashankari");
    mylist.add("Adugodi");
    ArrayAdapter adapt = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, mylist);
    myarea.setAdapter(adapt);

}
private void initializeVars() {
    // TODO Auto-generated method stub
    firstname = (EditText) findViewById(R.id.fname);
    lastname = (EditText) findViewById(R.id.lname);
    mycity = (EditText) findViewById(R.id.city);
    myphone = (EditText) findViewById(R.id.phone);
    password = (EditText) findViewById(R.id.paswrd);
    cpassword = (EditText) findViewById(R.id.cpaswrd);
}


public void onClick(View arg0) {
    switch(arg0.getId()) {
    case R.id.linktologin:
        Intent in = new Intent(MainActivity.this,Register.class);
        startActivity(in);
        break;

    case R.id.register:
        Boolean diditwork=true;
        try{
            EditText firstname = (EditText) findViewById(R.id.fname);
            String name=firstname.getText().toString();
            if (name.matches("")) {
                Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
                return;
            }
            EditText mycity = (EditText) findViewById(R.id.city);
            String mycty=mycity.getText().toString();
            if (mycty.matches("")) {
                Toast.makeText(this, "You did not enter your city", Toast.LENGTH_SHORT).show();
                return;
            }
            EditText myphone = (EditText) findViewById(R.id.phone);
            String ph=String.valueOf(myphone);
            if (ph.matches("")) {
                Toast.makeText(this, "You did not enter your phone number", Toast.LENGTH_SHORT).show();
                return;
            }
            EditText password = (EditText) findViewById(R.id.paswrd);
            String psd=password.getText().toString();
            if (psd.matches("")) {
                Toast.makeText(this, "You did not enter password", Toast.LENGTH_SHORT).show();
                return;
            }
            cpassword = (EditText) findViewById(R.id.cpaswrd);
            String cpsd=cpassword.getText().toString();
            if (cpsd.matches("")) {
                Toast.makeText(this, "please confirm your password", Toast.LENGTH_SHORT).show();
                return;
            }

            String lname=lastname.getText().toString();
            String bg =bloodgroup.getSelectedItem().toString();
            String are= myarea.getSelectedItem().toString();


            Database entry = new Database(MainActivity.this);
            entry.open();
            entry.createEntry(name, lname, bg, are, mycty, ph, psd, cpsd);
            entry.close();

        }
        catch (Exception e){
            diditwork=false;
            String error=e.toString();
            Dialog d=new Dialog(this);
            d.setTitle("Somewhere you are wrong jus check..");
            TextView tv=new TextView(this);
            tv.setText("Sucess");
            d.setContentView(tv);
            d.show();
            break;
        }

    }}  }

And thanks

Spring Breaker
  • 8,233
  • 3
  • 36
  • 60

6 Answers6

1

do this :

button.setEnabled(false);
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
1

Use :

myButton.setEnabled(false);

Also, android:clickable can be used via xml to set whether a button can be clickable or not.

Check this link here

Community
  • 1
  • 1
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
0

After checking for empty field,just add this line

sign.setEnabled(false);

for the sign button and

log.setEnabled(false)

for the log button.

mungaih pk
  • 1,809
  • 8
  • 31
  • 57
0

You have to add listeners to all views. After each edit you should check results and update button state with setEnabled(boolean).

Zielony
  • 16,239
  • 6
  • 34
  • 39
0

Use below code to achieve your task :-

    firstname.addTextChangedListener(new TextWatcher()
    {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            // TODO Auto-generated method stub
            if(s.length()>0)
            {
                log.setEnabled(false);
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            // TODO Auto-generated method stub
        }
    });
duggu
  • 37,851
  • 12
  • 116
  • 113
0

Continuing from @Siddharth's answer, I would like to add some more code.

I saw that you have a bunch of EditText which you wanted to validate.If you want to enable the register button when all compulsory fields are filled up then the solution would be to add a global listener to it.

Example:

Assign activity's root view a known ID, say '@+id/activityRoot', hook a GlobalLayoutListener into the ViewTreeObserver

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
       //Give your all  validation conditions here
       if(firstname.getText().toString().equals("")
         {
          button.setEnabled(false);
         }

     }
});

You can give all EditText conditions there which ever is compulsory for you.Make sure to initialize the EdiText before this otherwise you will getNullPointerException.

reference http://developer.android.com/reference/android/view/ViewTreeObserver.html

https://stackoverflow.com/a/4737265/1665507

Community
  • 1
  • 1
Spring Breaker
  • 8,233
  • 3
  • 36
  • 60