Reputation: 359
I'm making an app and I need to register user. I also have two checkboxes where the user selects gender (male or female). I know that checkbox understand boolean values true or false, but I'm wondering how to do that with firebase. This is exactly what I need to do: image
Here what I've done:
private void SignUp(final String firstName, final String lastName, final String email, final String password, final String checkMale, final String checkFemale) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
assert user != null;
String userId =user.getUid();
reference = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("id", userId);
hashMap.put("firstName", firstName);
hashMap.put("lastName", lastName);
hashMap.put("email", email.toLowerCase());
if(male.isChecked() && female.isChecked()) {
hashMap.put("male", checkMale);
hashMap.put("female", checkFemale);
}
reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()) {
pd.dismiss();
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Toast.makeText(RegisterActivity.this, "User Registered successfully", Toast.LENGTH_SHORT).show();
}
}
});
} else {
pd.dismiss();
Toast.makeText(RegisterActivity.this, "You can't register with this email or password", Toast.LENGTH_SHORT).show();
}
}
});
}
Any help is appreciated.
Upvotes: 1
Views: 1605
Reputation: 2386
Use Radio Buttons in place of the checkbox.
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rdgGender"
android:orientation="vertical">
<RadioButton android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
get RadioGroup id
RadioGroup rdgGender = findViewbyId(R.id.rdgGender);
on submit button click
public void onSubmitClick() {
boolean checkedId = rdgGender.getCheckedRadioButtonId();
switch(checkedId) {
case R.id.rbMale:
hashMap.put("gender", "Male");
break;
case R.id.rbFemale:
hashMap.put("gender", "Female");
break;
}
}
Upvotes: 0
Reputation: 351
You can use Radio Buttons instead of checkbox.
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/malleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
And in code you can check which is selected.
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.maleButton:
if (checked)
hashMap.put("male", true);
break;
case R.id.femaleButton:
if (checked)
hashMap.put("male", false);
break;
}
}
Also you do not need to store 2 variables in firebase male and female if users can choose only one. In other case(if users can choose both) store 2 variables and remove radiogroup so users can choose both.
Upvotes: 1
Reputation: 1084
I think in gender both can't checked at once and you kept && condition, so I would't work here:
if(male.isChecked() && female.isChecked()) {
hashMap.put("male", checkMale);
hashMap.put("female", checkFemale);
}
First of all do not need to save both value male & female. Just keep once key "gender" and specify one string or number e.g. 1 or "m" for male & 2 or "f" for female and save it along with all value.
Here you can use RadioButton inside RadioGroup 'gender' instead of only RadioButtons.
For your reference:
if(male.isChecked())
hashMap.put("gender", "m");
else
hashMap.put("gender", "f");
Upvotes: 0