Trouble converting an editable to a string (username/password test) - android

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).

Related

Check if EditText's content is settings

If the user clicks the "go" button, the application should check if the EditText's value is "Settings" or not? How can I do this?
Something like this:
Button buttn1;
EditText Text1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttn1 = (Button)findViewById(R.id.Button111);
Text1 = (EditText)findViewById(R.id.Text111);
buttn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
if (Text1 == "Settings") {
//CODE
}else {
//CODE
}
}
});
}
You can get the content of an EditText field like this: Text1.getText().toString() and you can check String equality using .equals().
So combined, this would be Text1.getText().toString().equals("Settings")
Try This:
if (Text1.getText().toString().equals("Settings")) {
//CODE
}else {
//CODE
}

Authenticating a User in android (Hard Coded)

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!");
}
}

Registration SignUp into my android app

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

android - Corrected text in EditText not received

I have two date fields (no, not the pickers) as fromDate and toDate. When I click on my Submit button, in the onResume(), I have validations in place for both date fields. When I enter an invalid value (not from syntax, locale, etc. point of view) for toDate and click Submit, I correctly see the toast. Then, I enter the correct the value and click Submit. The toast still appears ! In other words, the corrected date value is not being received.
I guess, I am missing the activity life-cycle w.r.t. toasts. (Each Toast is immediately followed by a return.) Can you please suggest what should be the correct flow to handle this error followed by correction ?
public class MainActivity extends Activity {
private Locale l ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set locale;
l = getResources().getConfiguration().locale;
}
#Override
protected void onResume() {
super.onResume();
addButtonListener();
}
private void addButtonListener() {
Button submitButton = (Button) findViewById(R.id.buttonSubmit);
submitButton.setOnClickListener(new OnClickListener() {
EditText fromDateText = (EditText) findViewById(R.id.fromDate);
EditText toDateText = (EditText) findViewById(R.id.toDate);
#Override
public void onClick(View v) {
String s;
if (fromDateText.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), R.string.err_fromDate_1, Toast.LENGTH_SHORT).show();
return;
}
if (toDateText.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), R.string.err_toDate_1, Toast.LENGTH_SHORT).show();
return;
}
}
});
}
}
Edited to add source code.
Make fromDateText and toDateText class variables and init them in onCreate.

Authentication Android

I want to create a static test in my authentication but when I click on button "Valider" nothing happen, instead when I remove the "if" condition and I click on "valider" button the next activity start. I think there is a problem when I put a condition for testing but I don't know what's it. Can you help ? thanks
public class TabAdmin extends Activity implements View.OnClickListener{
private EditText username;
private EditText password;
public String user_name;
public String pass_word;
private Button valider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authen);
username = (EditText) findViewById(R.id.login);
password = (EditText) findViewById(R.id.pwd);
valider = (Button) findViewById(R.id.valider);
valider.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
if (v == valider) {
user_name = username.getText().toString();
pass_word = password.getText().toString();
if((user_name=="admin")&&(pass_word=="admin"))
{
Intent goToNextActivity = new Intent(getApplicationContext(), MenuAdmin.class);
startActivity(goToNextActivity);
}
}
}
try using if (v.equals(valider)) instead of if (v == valider)
For one thing the way you're comparing strings isn't right for Java. Try
if ("admin".equals(user_name) && "admin".equals(pass_word)) {
This isn't a great way of doing passwords though as anyone can read the strings out of the APK.
instead of
if((user_name=="admin")&&(pass_word=="admin"))
use
if((user_name.equals("admin"))&&(pass_word.equals("admin")))
I hope it can help u

Categories

Resources