
Hello guys, welcome back with another tutorial of android studio.
Here in this post, I am sharing one source code to make a login application where you will have to validate Email ID and Password.
while creating simple login validation then you have to mostly follow some steps :
Read Also –> Change the color of the screen, based on selected options from the menu.
Steps | Description |
1 | create a project in android studio with any name. |
2 | Modify src/MainActivity.java and add the code |
3 | Modify layout XML file res/layout/activity_main.xml |
Content of src/MainActivity.java
package com.example.kenilvavaliya.demo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText passwd;
private Button loginbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.editText2);
passwd = (EditText) findViewById(R.id.editText3);
loginbutton= (Button) findViewById(R.id.button);
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (username.getText().toString().equals("admin") &&
passwd.getText().toString().equals("kenil")) {
Toast.makeText(getApplicationContext(),
"Successfully login...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please enter valid username and password", Toast.LENGTH_LONG).show();
}
}
});
}
}
Content of res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="103dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="73dp" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="203dp"
android:text="Get access" />
</RelativeLayout>
Output: as a result, you can see
Type username: admin
password: kenil
You can see the message: – Successfully login… It seems like this
For any query please comment below in comment box.
Stay Connect with our app: –
https://play.google.com/store/apps/details?id=com.edu.easyengineer
For Assignment Questions and Explanation of a theory topic visit:
Be the first to comment