How to set my username and password in the code.? - android

Button loginbuttonbutton = (Button) findViewById(R.id.btnLogin);
loginbuttonbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(inputEmail.getText().toString() == "EdEffort#ncat.edu" &&
inputPassword.getText().toString() == "Steelers") {
Intent myIntent = new Intent(view.getContext(),
Host_Setting_PageActivity.class);
startActivityForResult(myIntent, 0);
} else {
System.out.println("Username or password is incorrect");
}
}
});
That is my code and the application actually start but whenever I hit the login button the application closes.

first use .equals() to compare strings.
== compares string refrences.Not value.
.equals() = compare strings character equality
if((inputEmail.getText().toString().equals("EdEffort#ncat.edu")&&inputPassword.getText().toString().equals("Steelers"))
And if force close than put logcat here..

use equals insead of == for String comparison
loginbuttonbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(inputEmail.getText().toString().equals("EdEffort#ncat.edu") && inputPassword.getText().toString().equals("Steelers") ){
Intent myIntent = new Intent(YOUR_CURRENT_ACTIVITY.this, Host_Setting_PageActivity.class);
startActivityForResult(myIntent, 0);
}
else{
System.out.println("Username or password is incorrect");
}
}
});
and make sure you are registering YOUR_CURRENT_ACTIVITY.this and Host_Setting_PageActivity.class both in manifest.xml

You should add
Host_Setting_PageActivity.class to your AndroidManifest
And also, you should compare strings always using .equals and not with "==" as "==" will really compare the instance of the string object and .equals will check the value of the string

dont use == method in if Condition but used .equals() method
like as
if(inputEmail.getText().toString().equals("abc") && inputPassword.getText().toString().equals("abc") ){
----------your code here-----------
}

Related

Android studio login system

i am trying to create a login system on android studio using java, i have tried a piece of code i have found and modifies it to my own program- i am getting errors that the tutorial cannot explain and would appreciate if someone could tell me what i'm doing wrong.
on line 10 under username it says expression expected
The else statement says there should be an if but as you can see there is ?
public void LoginButton(){
UserName = findViewById(R.id.UserName);
userPassword = findViewById(R.id.userPassword);
userPin = findViewById(R.id.userPin);
GoBtn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (UserName.getText().toString().equals("user");// here i would preferably link to a database but do to time restaitns i have modeled it with user
userPassword.getText().toString().equals("pass");
{
Toast.makeText(MainActivity.this, "Username and password is correct",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, StudentActivity.class);
startActivity(intent);
}
else {
Toast.makeText(MainActivity.this,"Username and password is NOT correct",
Toast.LENGTH_SHORT).show();
}
}
I need this to lead the user to the next activty if the input is correct but so far i cannot get it to run due to the errors.
Issues
You can't use a semicolon (;) right after if statement because an empty ; is also considered a statement.
If Statement is incorrect: It has to be a valid statement
Many braces ({ and }) are missing
Here is a sample code
public void LoginButton() {
UserName = findViewById(R.id.UserName);
userPassword = findViewById(R.id.userPassword);
userPin = findViewById(R.id.userPin);
GoBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (UserName.getText().toString().equals("user") &&
userPassword.getText().toString().equals("pass")) {
// use your code
} else {
// use your code
}
}
});
}
Suggestion: Please read more Java
your if condition syntax is wrong you need to use AND operator(&&).
change these lines
if (UserName.getText().toString().equals("user");
userPassword.getText().toString().equals("pass");
to
if((UserName.getText().toString().equals("user")) && (userPassword.getText().toString().equals("pass")))

Android Questions App Answer==MyAnswer not null

I make android questions app. I can not find the right answer. Answer list.
Right answer: String answer = quizList.get(position).getAnswer();
Button Click:
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (answer =="A"){
lblsignboard.setText("Ok");
}
else
{
lblsignboard.setText("No");
}
}
});
Answer = "A", but this code does not work. Always gives the answer is no.
When you are comparing strings in java,always use .equals
Because the function (.equals) checks the actual contents of the string, the == operator checks whether the references to the objects are equal.
and now furthur using equalsignorecase you need not to worry about search string is in caps or small.
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (equalsIgnoreCase("a")){
lblsignboard.setText("Ok");
}else{
lblsignboard.setText("No");
}
}
});
You can use this way too, to keep you code working even the answer key is of different case. Use either equals("A") or equalsIgnoreCase("a").
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (answer.equals("A") || equalsIgnoreCase("a")){
lblsignboard.setText("Ok");
}else{
lblsignboard.setText("No");
}
}
});
In Android we make use of two things to compare strings:
1. equals : this is used when you want to compare even case of the string.
2. equalsIgnoreCase: This is just for content comparison.
Here is the small illustration as how it works:
String myString = "heLLo";
if(myString.equals("hello"))
{
System.out.print("equals");
}
else if(myString.equalsIgnoreCase("hello"))
{
System.out.print("equalsIgnoreCase");
}
Here equalsIgnoreCase will be printed.
Strings can only compare by .equals method not by ==. Also you have to check string is null or not because .equals method may invoke
nullpointerexception and you app may crash.
buttonA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(answer!=null &&(answer.equals("A")||equalsIgnoreCase("a"))) {
lblsignboard.setText("Ok");
} else {
lblsignboard.setText("No");
}
}
});
String can compare is .equals or .eqalignorecase not a '==' sign.'==' using a int value compare.
String answer=item.get(position).getAnswer();
if(answer!=null && (answer.equals("A")||equalsIgnoreCase("a")){
Log.e("Comapare","----------Success");
}else{
Log.e("Comapare","----------not compare");
}

comparing two strings in Android ( if-else statemente)

I am working on a login page in an Android App.
As you know, the app must check if the username and password are valid, and then grant the user access to the application.
I have used the following code:
...
EditText un = (EditText) findViewById(R.id.username1);
EditText pw = (EditText) findViewById(R.id.password1);
String u = un.getText().toString();
String p = pw.getText().toString();
//////// Now on the click of the Login Button:
public void onClickL (View view){
if ( (u.equals("Android")) && (p.equals("1234"))) /////// move to a new activity
else ///////Display a warning message: Try again
}
when I run this code this only executes the else part. why its not executing the if part? what should I do ?
The reason is that you are fetching the EditText's value while declaring the EditText. Actually you nee to fetch the Text from EditText while clicking on the button, hence you need to move you code to onClick() method like below,
#Override
public void onClick (View view)
{
String u = un.getText().toString();
String p = pw.getText().toString();
if ( (u.equals("Android")) && (p.equals("1234"))) /////// move to a new activity
{
....
}
else ///////Display a warning message: Try again
{
....
}
}
please try this:
public void onClickL (View view){
u = un.getText().toString();
p = pw.getText().toString();
if ( u.equals("Android") && p.equals("1234") ) /////// move to a new activity
{
}
else ///////Display a warning message: Try again
{
}
}
try Following code
need to clear space in username if it is available.
public void onClick (View view){
String username = un.getText().toString().trim();
String password = pw.getText().toString();
if ((username.equals("Android")) && (password.equals("1234"))) {
//do something
} else{
//do something
}
}
Try this..
get the text inside Click function like below
public void onClick (View view){
String u = un.getText().toString().trim();
String p = pw.getText().toString().trim();
if ((u.equals("Android")) && (p.equals("1234"))) {
//do something
}
else{
//do something
}
}

empty field error only first block execute

public void save_record()
{
name_val = name.getText().toString();
pass_val = password.getText().toString();
cpass_val = cpassword.getText().toString();
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(name_val.equals(null)==true || pass_val.equals(pass_val)==true || cpass_val.equals(cpass_val)==true)
{
Toast.makeText(getApplicationContext(), "Complete Text Field",Toast.LENGTH_LONG).show();
}
}
});
}
if all fields are completed or not only first block execute... please tell me anser
here you are using || between your conditions so if all fields are empty then
if(name_val.equals(null)==true) is going to be executed and if all the fields are filled then one of the other two will execute for sure because you are comparing
pass_val.equals(pass_val)==true and cpass_val.equals(cpass_val)==true which is always going to be true so check your conditions and then try

Login not working

I have written the simple code for Login authentication with hardcoded password.my problem is evenif I am entering the correct password my control is going in elese loop
edt=(EditText)findViewById(R.id.edt);
btn=(Button)findViewById(R.id.sub);
s1=edt.getText().toString();
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("mynameeeeee",s1);
if(s1=="123")
{
Toast.makeText(getApplicationContext(), "Successful",Toast.LENGTH_LONG).show();
}
else
{
Log.d("coming in elseeeee","coming in elseeeee");
Toast.makeText(getApplicationContext(), "not valid",Toast.LENGTH_LONG).show();
}
}
});
Here's the problem :
You are storing a reference of the edit text content at creation time, when the edit text is empty.
You should retrieve the content of the edit text EVERYTIME you want to compare, which is when the button is clicked in your case :
Do the following :
edt=(EditText)findViewById(R.id.edt);
btn=(Button)findViewById(R.id.sub);
btn.setOnClickListener ( new OnClickListener () {
#Override
public void onClick ( View v ) {
Log.d ( "mynameeeeee" , edt.getText().toString() );
if ( edt.getText().toString().equals ( "123" ) )
{
Toast.makeText(getApplicationContext(), "Successful",Toast.LENGTH_LONG).show();
}
else
{
Log.d("coming in elseeeee","coming in elseeeee");
Toast.makeText(getApplicationContext(), "not valid",Toast.LENGTH_LONG).show();
}
}
});
the string should be compared like:
if(s1.equals("123")) {}
Change your if statement like this
if(s1.equals("123"))
{
Toast.makeText(getApplicationContext(), "Successful",Toast.LENGTH_LONG).show();
}
else
{
Log.d("coming in elseeeee","coming in elseeeee");
Toast.makeText(getApplicationContext(), "not valid",Toast.LENGTH_LONG).show();
}
When comparing strings always use .equals() function
== Checks whether both the variable are referring to same object. In this case since they are referring to different object so the result of == is false.
use equals() method s1.equals("123") to check the content of the string object.

Categories

Resources