If i click on login button my username and password is same as condition but get login failed in Toast Message
. Insert correct username and password compiler going to else part. I don’t know what is the problem. I am stuck from 4 hours. If you know please solve i am a newbie.
Here down is my code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Username=(TextView) findViewById(R.id.Username);
TextView password=(TextView) findViewById(R.id.password);
Button loginbtn=(Button) findViewById(R.id.Button);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Username.getText().toString().equals("ADMIN") && password.getText().toString().equals("ADMIN")) {
Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getApplicationContext(), "Login Faild!!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backs"
tools:context=".MainActivity">
<TextView
android:id="@+id/Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Sign in"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:background="#FFFFFF"
android:drawableLeft="@drawable/ic_baseline_person_outline_24"
android:drawablePadding="8dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:minHeight="48dp"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Username" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:background="#FFFFFF"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:drawablePadding="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edittext" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Forgot password"
android:textColor="#000000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.837"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
<Button
android:id="@+id/Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="28dp"
android:layout_marginRight="30dp"
android:background="@drawable/bg"
android:text="@string/login"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="288dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="or sign in with"
android:textColor="#BDB9B9"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/other"
app:layout_constraintVertical_bias="1.0">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="20dp"
android:src="@drawable/google" />
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:src="@drawable/facebook" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="20dp"
android:src="@drawable/twitter" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT
2
Answers
You are referring to the
R.id.Username
component which is theSIGN UP
text and you are casting the fields asTextView
s instead ofEditText
s.Change your variables to:
You are getting the text from the wrong reference, Change the below code to later.
to