I need to do a registration signup into my android application.So far I done it like this:
MainActivity.java:
public class MainActivity extends Activity {
private EditText mName;
private EditText mEmail;
private EditText mAge;
private Button mSubmit;
// Form used for validation
private Form mForm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFields();
initValidationForm();
initCallbacks();
}
private void initFields() {
mName = (EditText) findViewById(R.id.name);
mEmail = (EditText) findViewById(R.id.email);
mAge = (EditText) findViewById(R.id.age);
mSubmit = (Button) findViewById(R.id.submit);
}
private void initValidationForm() {
mForm = new Form(this);
mForm.addField(Field.using(mName).validate(NotEmpty.build(this)));
mForm.addField(Field.using(mEmail).validate(NotEmpty.build(this)).validate(IsEmail.build(this)));
mForm.addField(Field.using(mAge).validate(InRange.build(this, 0, 120)));
}
private void initCallbacks() {
mAge.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit();
return true;
}
return false;
}
});
mSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
submit();
}
});
}
private void submit() {
FormUtils.hideKeyboard(MainActivity.this, mAge);
if (mForm.isValid()) {
// Crouton.makeText(this, getString(R.string.sample_activity_form_is_valid), Style.CONFIRM).show();
Toast.makeText(this, getString(R.string.sample_activity_form_is_valid), Toast.LENGTH_LONG).show();
}
}
}
I couldn't able to get an exact registration sign up(Name,email,password,Re-enter password) for android login.Anybody can help me with this.If I get some tutorial or source code related to this.its enough for me.Thank you.
First know, for any registration you have to maintain the database where you can store your login credentials to check back.
Here are two links, which uses the sqlite database for storing and retreiving the signup details.
1.android-login-registration-screen-with-sqlite-database-example
2.android-code-for-user-registration
If you want to use mysql database for your registration you need to know json parsing.
Thanks
Related
I am studying Mobile Application Developer and I am just a beginner. So, forgive me if the question is silly.
I am creating a simple Quiz App. I have MainActivity which act as a welcoming screen.
public class MainActivity extends AppCompatActivity {
private Button startBt; //the user will enter this button to go the SecondActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBt = findViewById(R.id.startBt);
startBt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
}
}
Then, I have a SecondActivity which suppose to ask the user a question and get the answer as a String and then increment the score if the answer is correct. There is 10 question. So, I want to use a loop to iterate through all questions. Also, inside the loop there is OnKeyListener which indicate when the user press Enter and compare the user answer to the saved answer.
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String[] questions = getResources().getStringArray(R.array.questions);
String[] answers = getResources().getStringArray(R.array.answers);
TextView questionTv = findViewById(R.id.questionTv);
final EditText answerEt = findViewById(R.id.answerPt);
TextView scoreTv = findViewById(R.id.scoreTv);
int scoreCounter = 0;
for (int i=0; i<10; i++) {
answerEt.setText("");
scoreTv.setText(scoreCounter);
questionTv.setText(questions[i]);
answerEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (**answerEt**.getText().toString().equals(**answers[i]**)) {
Toast t = Toast.makeText(getApplicationContext(), "Correct",Toast.LENGTH_LONG);
t.show();
**scoreCounter++;**
return true;
}
else
return false;
} else
return false;
}
});
}
}
}
Now, please check the bold part of the code. I cannot access answerEt, scoreCounter, answer[i] because they are not final. They should be final because I am working with an inner class. Here is the problem, if I will make them final, I will not have the ability to modify them. However, scoreCounter should be incremented to show the user score. Also the i should be incremented to iterate through the answer[]. One will say "make them Global". Sadly, I tried this and my program crashed. So, How I can solve this?
Thanks in advance
pass on responsibility to check for correct answer to a separate function.
private boolean checkAnswer(EditText et, final int questionID) {
if (et.getText().toString().equals(answers[questionID])) {
Toast t = Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_LONG);
t.show();
scoreCounter++;
return true; // for correct answer.
}
return false; // for incorrect answer.
}
Modify your loop to use above function like this :
for (int i = 0; i < 10; i++) {
final int currentQuestionID = i;
answerEt.setText("");
scoreTv.setText(scoreCounter);
questionTv.setText(questions[i]);
answerEt.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
return checkAnswer((EditText) (v), currentQuestionID);
}
return false;
}
});
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i have to two table "OWNER" and "USER" on selecting the first radio button it save the data on user table and on selecting second radio button data will be save on owner table
database helper class which contain the function to create the user table and owner table and insertdataUse insertdataOwner funtion to insert the data in table
here is my singup page in which we have two function adddatauser to user and adddataowner to
public class SinGUP extends AppCompatActivity {
Databasehelper myDB;
EditText et_name,et_pnumber,et_email,et_password;
private static Button btnsignup;
String name,pnumber,email,password;
RadioButton r1,r2;
RadioGroup rg;
public int x;
TextView loginbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sin_gup);
myDB = new Databasehelper(this);
et_name=(EditText)findViewById(R.id.editText_pname);
et_pnumber=(EditText)findViewById(R.id.editText_pnumber);
et_password=(EditText)findViewById(R.id.editText_password);
et_email=(EditText)findViewById(R.id.editText_email1);
r1=(RadioButton)findViewById(R.id.radioButton_Owner);
r2=(RadioButton)findViewById(R.id.radioButton_user);
loginbtn=(TextView)findViewById(R.id.textView_login);
//TypeOfUser();
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i= new Intent(SinGUP.this,MainActivity.class);
startActivity(i);
}
});
btnsignup=(Button)findViewById(R.id.button_singup);
btnsignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
singup();
}
});
}
public void singup()
{
initializes();
if(!Validate())
{
Toast.makeText(SinGUP.this,"sigup can't be done",Toast.LENGTH_LONG).show();
}
else {
rg = (RadioGroup) findViewById(R.id.radiogroup);
int selectOptId = rg.getCheckedRadioButtonId();
r1 = (RadioButton) findViewById(selectOptId);
Toast.makeText(getApplicationContext(), r1.getText(), Toast.LENGTH_SHORT).show();
if (r1.getText() == "radioButton_user")
Adddatauser();
if (r1.getText() == "radioButton_owner")
AdddataOwner();
}
}
public void initializes()
{
name=et_name.getText().toString().trim();
pnumber=et_pnumber.getText().toString().trim();
email=et_email.getText().toString().trim();
password=et_password.getText().toString().trim();
}
public boolean Validate()
{
boolean valid=true;
if(name.isEmpty()||name.length()<7){
et_name.setError("please enter the valid name");
valid=false;}
if(pnumber.isEmpty()||pnumber.length()<10){
et_pnumber.setError("please enter the valid phone number");
valid=false;}
if(password.isEmpty()||password.length()<7){
et_password.setError(" password length 8");
valid=false;}
if(email.isEmpty()||!isValidEmail(email)){
et_name.setError("please enter the valid email");
valid=false;}
return valid;
}
public boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)#"+"[A-Za-z0-9-]+(\.[A-Za-z0-9]+)(\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
//function to add owner detail
public void AdddataOwner(){
boolean inserData = myDB.insertdataOwner(name, pnumber, password, email);
// boolean insertDataU = myDB.insertdataUser(name.
getText().toString(), pnumber.getText().toString(), password.getText().toString(), email.getText().toString());
if (inserData == true)
Toast.makeText(SinGUP.this, "data inserted owner", Toast.LENGTH_SHORT).show();
else
Toast.makeText(SinGUP.this, "data canot be inserted owner", Toast.LENGTH_SHORT).show();
}
//function to add user detail
public void Adddatauser(){
boolean insertDataU = myDB.insertdataUser(name, pnumber, password, email);
if (insertDataU==true)
Toast.makeText(SinGUP.this, "data inserted user", Toast.LENGTH_SHORT).show();
else
Toast.makeText(SinGUP.this, "data canot be inserted user", Toast.LENGTH_SHORT).show();
}
Change below condition
if (r1.getText() == "radioButton_user")
Adddatauser();
if (r1.getText() == "radioButton_owner")
AdddataOwner();
to
if (r1.getText().equalsIgnoreCase("radioButton_user")) {
Adddatauser();
} else if (r1.getText().equalsIgnoreCase("radioButton_owner")) {
AdddataOwner();
}
hi i am working on a android project where i am using firebase as back-end and i am building a signup and login form . When ever i sign up the code is working well and . When i try to retrieve it using "signInWithEmailAndPassword i am getting the fallowing error. The email address is badly formatted Firebase`
login Activity
public class LoginActivity extends AppCompatActivity {
private EditText mLoginEmailField;
private EditText mloginPassField;
private Button mLoginbtn;
private Button mNewAccountbtn;
private DatabaseReference mDatabaseRefrence;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
mLoginEmailField = (EditText) findViewById(R.id.loginEmailField);
mloginPassField = (EditText) findViewById(R.id.loginPasswordField);
mLoginbtn = (Button) findViewById(R.id.loginBtn);
mNewAccountbtn = (Button) findViewById(R.id.newAccountbtn);
mDatabaseRefrence = FirebaseDatabase.getInstance().getReference().child("Users");
mNewAccountbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent rigisterIntent = new Intent(LoginActivity.this,RigisterActivity.class);
rigisterIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(rigisterIntent);
}
});
mLoginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckLogin();
}
});
}
private void CheckLogin() {
String email = mloginPassField.getText().toString().trim();
String pass = mloginPassField.getText().toString().trim();
if(!TextUtils.isEmpty(email) && !TextUtils.isEmpty(pass)){
mAuth.signInWithEmailAndPassword(email,pass).addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
CheackUserExsists();
}else{
System.out.println("Sign-in Failed: " + task.getException().getMessage());
Toast.makeText(LoginActivity.this,"Erorr Login",Toast.LENGTH_LONG).show();
}
}
});
}
}
private void CheackUserExsists() {
final String user_id = mAuth.getCurrentUser().getUid();
mDatabaseRefrence.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(user_id)){
Intent MainIntent = new Intent(LoginActivity.this,MainActivity.class);
MainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(MainIntent);
}else
{
Toast.makeText(LoginActivity.this,"You need to setup your Account.. ",Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Rigister Actvity
public class RigisterActivity extends AppCompatActivity {
private EditText mNameField;
private EditText mPassField;
private EditText mEmailField;
private Button mRigisterbtn;
private ProgressDialog mProgres;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rigister);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mAuth = FirebaseAuth.getInstance();
mProgres = new ProgressDialog(this);
mNameField = (EditText) findViewById(R.id.nameField);
mPassField = (EditText) findViewById(R.id.passFiled);
mEmailField = (EditText) findViewById(R.id.emailField);
mRigisterbtn = (Button) findViewById(R.id.rigisterbtn);
mRigisterbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StartRigister();
}
});
}
private void StartRigister() {
final String name = mNameField.getText().toString().trim();
String pass = mPassField.getText().toString().trim();
String email = mEmailField.getText().toString().trim();
if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pass) && !TextUtils.isEmpty(email)){
mProgres.setMessage("Signing Up... ");
mProgres.show();
mAuth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference CurentUser_db = mDatabase.child(user_id);
CurentUser_db.child("name").setValue(name);
CurentUser_db.child("image").setValue("defalut");
mProgres.dismiss();
Intent mainIntent = new Intent(RigisterActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
}
}
});
}
}
}
I have made sure that i have setup email and password active in the auth section of firebase.
still firebase giving me the following error.
Your code to set email is incorrect. You are setting email to the value of the EditText for password.
In method CheckLogin(), change:
String email = mloginPassField.getText().toString().trim();
to:
String email = mLoginEmailField .getText().toString().trim();
Simply use Edittext named as Email and Password you need not do anything.
the error comes up only if you use plaintext for both...
I faced this problem recently, possible solutions are :
Check the inputType of your EditText Field.
ADD this attribute to your EditText
android:inputType="textEmailAddress"
In Activity class, it should look like if u are using TextInputLayout instead of editText
mDisplayName=(TextInputLayout) findViewById(R.id.reg_name);
mDisplayEmail=(TextInputLayout)findViewById(R.id.reg_email);
mDisplayPassword=(TextInputLayout)findViewById(R.id.reg_password);
String name = mDisplayName.getEditText().getText().toString();
String email = mDisplayEmail.getEditText().getText().toString();
String password = mDisplayPassword.getEditText().getText().toString();`
Remove whitespaces from email text it worked for me. by using trim() method you can remove spaces.
The error popped for me due to my silly action of using "tab" to go to the next text field. Don't use "tab" for it, instead use your mouse to move to the next text field. Worked for me.
What helped me resolve this issue is to put the android:id into the correct place.
If you are using Material design, there are two parts of your text input, the layout and the actual functional part.
If you put the ID into the layout, you'll only be able to access editText property in the activity class, but if you put it in the functional part, you'll be able to access .text or getText() as someone above has stated.
Change
String pass = mloginPassField.getText().toString().trim();
mAuth.signInWithEmailAndPassword(email,pass)
to
String password = mloginPassField.getText().toString().trim();
mAuth.signInWithEmailAndPassword(email,password)
Your code to set email is incorrect.
Perhaps a space was used as the last letter.
final User? user = (await _auth.signInWithEmailAndPassword(
email: _emailController.text.toString().trim(),
password: _passwordController.text,
)).user;[![enter image description here][1]][1]
I am new to android.. Have learnt a few properties.. Trying to authenticate a user by comparing the entered string to a static , hard coded string . I am setting the text of the "Login" button to post the message as "Correct Password" or "Incorrect Password!" , but every time the "Incorrect Password!" message is only printed on the button.
Here's the code..
public class MainActivity extends Activity implements OnClickListener {
Button btnLogin;
EditText etUsername;
EditText etPassword;
TextView tvUname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = (EditText)findViewById(R.id.editText1);
etPassword = (EditText)findViewById(R.id.editText2);
btnLogin = (Button)findViewById(R.id.button1);
btnLogin.setOnClickListener(this);
}
and here is the OnClick method's code!
#Override
public void onClick(View v) {
String uname = "abhi";
String pass = "test";
if(uname == etUsername.getText().toString() && pass == etPassword.getText().toString()){
btnLogin.setText("Correct Password!");
}
else{
btnLogin.setText("Incorrect Password!");
}
}
}
Please Help me ..!
When you compare String use equals
You compare int, double, float, long and boolean with ==
Try this
if( (uname == etUsername.getText().toString() ) && ( pass == etPassword.getText().toString() )){
btnLogin.setText("Correct Password!");
}
Android luckily has TextUtils.equals() for text comparison
So you can do:
private boolean isAutheticated(CharSequence username, CharSequence password) {
String uname = "abhi";
String pass = "test";
//Check if they match and return the result
return TextUtils.equals(uname, username) && TextUtils.equals(pass, password);
}
#Override
public void onClick(View v) {
if(isAuthenticated(etUsername.getText(), etPassword.getText())) {
btnLogin.setText("Correct Password!");
} else {
btnLogin.setText("Incorrect Password!");
}
}
I'm trying to create a simple username/password login screen. I have the layout done, and right now, I'm trying to set it so when the username (EditText) == "crete", then it should do something. Here is my code...:
public class Login extends Activity {
public static EditText username, password;
public Button loginbutton;
boolean accessgranted;
public String dbu, dbp, user1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) this.findViewById(R.id.username);
password = (EditText) this.findViewById(R.id.password);
loginbutton = (Button) this.findViewById(R.id.loginbutton);
user1 = "crete";
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
dbu = (username.getText()).toString();
}
finally{
if (dbu == user1){
username.setText("SUCCESS");
}
}
}
});
}
}
this, sadly, doesn't work. It correctly converts it to a string (i think) because when I tested this code out :
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
dbu = (username.getText()).toString();
}
finally{
username.setText("done" + dbu);
}
}
}
});
It correctly enters what you entered into the EditText, plus the word "done".
There seems to be a problem with creating if-then statements??
You test for String equality with the method .equals("String").
With == you are testing if the references to the objects are equal.
Try using equalsIgnoreCase(String) instead of the == comparator.
Like this: dbu.equalsIgnoreCase(user1)
dub and user1 are two separate String objects. You're comparing them like this: dbu == user1. This will always return false. Instead, replace it with dbu.equals(user1).