0% found this document useful (0 votes)
20 views3 pages

t8NHJnuNz00vTGKkMV40GoN7H 4vjqg2 K - psPa9b0ROq2i - Vji61HUOLzzMJcx AyOZ Esr5GaH

Uploaded by

samegenda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

t8NHJnuNz00vTGKkMV40GoN7H 4vjqg2 K - psPa9b0ROq2i - Vji61HUOLzzMJcx AyOZ Esr5GaH

Uploaded by

samegenda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Create an application with login module. (Check username and password).

ANS -
Let's create a basic Android application with a login module to check the username
and password. Follow these steps:

Step 1: Create a New Project


Open Android Studio.

Click on Start a new Android Studio project.

Select Empty Activity and click Next.

Name your application, e.g., LoginApp.

Set the Save location, Language (Java or Kotlin), and the Minimum API level.

Click Finish.

Step 2: Design the Layout


Open the activity_main.xml file located in app/src/main/res/layout/.

Replace the existing XML with the following code to create a simple login form:

xml

Copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">

<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
Step 3: Update the Main Activity
Open the MainActivity.java or MainActivity.kt file located in
app/src/main/java/your_package_name/.
Implement the logic to check the username and password:

Java:
java

Copy
package your_package_name;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText username = findViewById(R.id.username);


EditText password = findViewById(R.id.password);
Button loginButton = findViewById(R.id.loginButton);

loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = username.getText().toString();
String pass = password.getText().toString();

if (user.equals("admin") && pass.equals("password")) {


Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid Username or
Password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Kotlin:
Java

Copy
package your_package_name

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val username = findViewById<EditText>(R.id.username)
val password = findViewById<EditText>(R.id.password)
val loginButton = findViewById<Button>(R.id.loginButton)

loginButton.setOnClickListener {
val user = username.text.toString()
val pass = password.text.toString()

if (user == "admin" && pass == "password") {


Toast.makeText(this, "Login Successful", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Invalid Username or Password",
Toast.LENGTH_SHORT).show()
}
}
}
}
Step 4: Run the Application
Connect your Android phone or start the emulator.

Click on the Run button (green triangle) in Android Studio.

Select your connected device or emulator and click OK.

The application will install and launch, displaying the login screen.

You might also like