0

Android, Using a Registration form linked to a Firebase db.How do i make all the Edittext fields mandatory?

Using a few edit Text fields, submit button should either be not visible /not functional until all the fields are filled,and User should also be not created.

Here's the code segment

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_firebase);
    //get data from previous activity
    Intent intent=getIntent();
    String RegAmount=intent.getStringExtra("regAmount");
    String Ename=intent.getStringExtra("Ename");
    //data recieved
    inputName = (EditText) findViewById(R.id.name);
    inputEmail = (EditText) findViewById(R.id.email);
    inputUsn = (EditText) findViewById(R.id.usn);
    inputEventName = (EditText) findViewById(R.id.EventName);
    inputEventName.setText(Ename);
    inputContact = (EditText) findViewById(R.id.phone);
    inputCollege = (EditText) findViewById(R.id.college);
    inputRegAmount = (EditText) findViewById(R.id.RegAmount);
    inputRegAmount.setText(RegAmount);
    SubmitForm = (Button) findViewById(R.id.RegButton);
    mFirebaseInstance = FirebaseDatabase.getInstance();
    mFirebaseDatabase = mFirebaseInstance.getReference("users");
    mFirebaseInstance.getReference("app_title").setValue("Saviskara Registration Form");
    // app_title change listener
    mFirebaseInstance.getReference("app_title").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            Log.e(TAG, "App title updated");

            String appTitle = dataSnapshot.getValue(String.class);

            // update toolbar title
            getSupportActionBar().setTitle(appTitle);
        }
        @Override
        public void onCancelled(DatabaseError error)
        {
            // Failed to read value
            Log.e(TAG, "Failed to read app title value.", error.toException());
        }
    });
    // Save the user
    SubmitForm.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String name = inputName.getText().toString();
            String email = inputEmail.getText().toString();
            String usn=inputUsn.getText().toString();
            String eventName=inputEventName.getText().toString();
            String contact=inputContact.getText().toString();
            String college =inputCollege.getText().toString();
            String regamount=inputRegAmount.getText().toString();
            createUser(name,email,usn,contact,regamount,eventName,college);
            Toast.makeText(Firebase.this,"Thank You!",Toast.LENGTH_SHORT).show();
            Intent i = new Intent(Firebase.this,MainActivity.class);
            startActivity(i);
            finish();

        }
    });
}
private void createUser(String name, String email,String usn,String pno,String regamount,String ename,String colName)
{
    // TODO

    userId = mFirebaseDatabase.push().getKey();

    User user = new User(name,email,usn,pno,regamount,ename,colName);

    mFirebaseDatabase.child(userId).setValue(user);

    addUserChangeListener();

}
private void addUserChangeListener()
{
    // User data change listener
    mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            User user = dataSnapshot.getValue(User.class);

            // Check for null
            if (user == null)
            {
                Log.e(TAG, "User data is null!");
                return;
            }

            // clear edit text
            inputEmail.setText("");
            inputName.setText("");
            inputCollege.setText("");
            inputContact.setText("");
            inputRegAmount.setText("");
            inputUsn.setText("");
            inputEventName.setText("");
        }
        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.e(TAG, "Failed to read user", error.toException());
        }
    });



}
}

Can I add a payment gateway via InstaMojo to the same and push into FireBase DB only when the payment is successful?

Also couldn't find anywhere the InstaMojo guide for firebase, just java python and php.

Does it mean I need a new backend ?

KENdi
  • 7,576
  • 2
  • 16
  • 31

3 Answers3

1

you can check validation like this for EditText:

SubmitForm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                if(checkValidation()) {
                    String name = inputName.getText().toString().trim();
                    String email = inputEmail.getText().toString().trim();
                    String usn = inputUsn.getText().toString().trim();
                    String eventName = inputEventName.getText().toString().trim();
                    String contact = inputContact.getText().toString().trim();
                    String college = inputCollege.getText().toString().trim();
                    String regamount = inputRegAmount.getText().toString().trim();
                    createUser(name, email, usn, contact, regamount, eventName, college);
                    Toast.makeText(Firebase.this, "Thank You!", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(Firebase.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        });

and checkValidation method will be:

  public  boolean checkValidation() {
        String name = inputName.getText().toString().trim();
        String email = inputEmail.getText().toString().trim();
        String usn = inputUsn.getText().toString().trim();
        String eventName = inputEventName.getText().toString().trim();
        String contact = inputContact.getText().toString().trim();
        String college = inputCollege.getText().toString().trim();
        String regamount = inputRegAmount.getText().toString().trim();

        if (name.length() <= 0) {
            inputName.requestFocus();
            inputName.setError("Enter Name");
            return false;
        } else if (email.length() <= 0) {
            inputEmail.requestFocus();
            inputEmail.setError("Enter Email");
            return false;

        } else if (usn.length() <= 0) {
            inputUsn.requestFocus();
            inputUsn.setError("Enter USN");
            return false;

        } else if (eventName.length() <= 0) {
            inputEventName.requestFocus();
            inputEventName.setError("Enter EventName");
            return false;

        } else if (contact.length() <= 0) {
            inputContact.requestFocus();
            inputContact.setError("Enter Contact");
            return false;

        } else if (college.length() <= 0) {
            inputCollege.requestFocus();
            inputCollege.setError("Enter Collage");
            return false;

        } else if (regamount.length() <= 0) {
            inputRegAmount.requestFocus();
            inputRegAmount.setError("Enter RegAmount");
            return false;

        } else {
            return true;
        }
    }

for submit button visibility, you can use TextWatcher with all EditText field.

aj0822ArpitJoshi
  • 1,142
  • 1
  • 9
  • 25
0

For your first question - Button should either be not visible/not functional until all the fields are filled:

You need to implement TextWatcher to check for input change in EditText (Example 1) or use OnTextChanged() using ButterKnife (Example 2).

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
-1

You can create a view below every edit text and can check if edit text is empty or not in a handler every 1500 ms and show view if it fulfill condition.

Handler handler = new Handler();
int delay = 1000; //milliseconds

handler.postDelayed(new Runnable(){
    public void run(){
        if(edittext1.isEmpty()){
view1.setvisibilty(View.VISIBLE);
}
else if(edittext2.isEmpty()){
view2.setvisibilty(View.VISIBLE);
}
and so on


        handler.postDelayed(this, delay);
    }
}, delay);
Mohit Hooda
  • 273
  • 3
  • 9