0% found this document useful (0 votes)
10 views4 pages

Android PRACTICAL.5

Uploaded by

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

Android PRACTICAL.5

Uploaded by

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

PRACTICAL.

5
To understand Activity, Intent Create sample application with login module.(Check
username and password).
MainActivity :
import android.content.Intent;

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 {

private EditText usernameEditText, passwordEditText;

private Button loginButton;

// Hardcoded username and password for demo


private static final String CORRECT_USERNAME = "user";
private static final String CORRECT_PASSWORD = "password";

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

usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {


String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();

if (username.equals(CORRECT_USERNAME) &&
password.equals(CORRECT_PASSWORD)) {
// If username and password are correct, open HomeActivity

Intent intent = new Intent(MainActivity.this, HomeActivity.class);


startActivity(intent);
finish(); // Finish current activity
} else {
// If username or password is incorrect, show a toast message

Toast.makeText(MainActivity.this, "Invalid username or password",


Toast.LENGTH_SHORT).show();
}
}
});
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<EditText

android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginBottom="16dp"/>

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:hint="Password"
android:inputType="textPassword"
android:layout_below="@id/usernameEditText"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"

android:layout_below="@id/passwordEditText"/>

</RelativeLayout>

HomeActivity:

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class HomeActivity extends AppCompatActivity {

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

}
}

activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"

tools:context=".HomeActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Welcome to Home Screen"


android:textSize="24sp"
android:layout_centerInParent="true"/>

</RelativeLayout>

You might also like