0% found this document useful (0 votes)
15 views6 pages

Q2

Uploaded by

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

Q2

Uploaded by

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

Q2. Java Android Program to demonstrate Registration form with validation.

// MainActivity.java
package com.example.pract_2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {

private EditText editTextLoginUsername, editTextLoginPassword;


private Button buttonLogin;
private TextView btn;

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

editTextLoginUsername = findViewById(R.id.editTextLoginUsername);
editTextLoginPassword = findViewById(R.id.editTextLoginPassword);
TextView btn=findViewById(R.id.Dont_Have_ac);
buttonLogin = findViewById(R.id.buttonLogin);

buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

loginUser();
}
});

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,MainActivity.class));
}
});

private void loginUser() {


// Retrieve input values
String username = editTextLoginUsername.getText().toString().trim();
String password = editTextLoginPassword.getText().toString().trim();

if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please enter username and password",
Toast.LENGTH_SHORT).show();
return;
}

if (username.equals("admin") && password.equals("admin123")) {


Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
startActivity(intent);

finish();

} else {

Toast.makeText(this, "Invalid username or password", Toast.LENGTH_SHORT).show();


}
}
}

// LoginActivity.java

package com.example.pract_2;

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

public class LoginActivity extends AppCompatActivity {

private EditText editTextLoginUsername, editTextLoginPassword;


private Button buttonLogin;
private TextView btn;

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

editTextLoginUsername = findViewById(R.id.editTextLoginUsername);
editTextLoginPassword = findViewById(R.id.editTextLoginPassword);
TextView btn=findViewById(R.id.Dont_Have_ac);
buttonLogin = findViewById(R.id.buttonLogin);

buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

loginUser();
}
});

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,MainActivity.class));
}
});
}

private void loginUser() {

String username = editTextLoginUsername.getText().toString().trim();


String password = editTextLoginPassword.getText().toString().trim();

if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please enter username and password",
Toast.LENGTH_SHORT).show();
return;
}
if (username.equals("admin") && password.equals("admin123")) {

Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);


startActivity(intent);
finish();

} else {

Toast.makeText(this, "Invalid username or password", Toast.LENGTH_SHORT).show();


}
}
}

//profileActivity.java

package com.example.pract_2;

import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class ProfileActivity extends AppCompatActivity {

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

Toast.makeText(ProfileActivity.this, "Login successful", Toast.LENGTH_SHORT).show();


}
}
activity_main

activity_login

activity_profile

You might also like