0% found this document useful (0 votes)
17 views13 pages

Slip 11

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

Slip 11 Q1 a

Design android application for login activity. Write android code to check login credentials with
username = "mca" and password = "android". Display appropriate toast message to the user.

1. Create the XML layout for the login activity (activity_login.xml):


xml
<!-- res/layout/activity_login.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:paddingLeft="16dp"

android:paddingTop="16dp"

android:paddingRight="16dp"

android:paddingBottom="16dp"

tools:context=".LoginActivity">

<EditText

android:id="@+id/usernameEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Username"/>

<EditText

android:id="@+id/passwordEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/usernameEditText"

android:layout_marginTop="8dp"

android:hint="Password"

android:inputType="textPassword"/>
<Button

android:id="@+id/loginButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/passwordEditText"

android:layout_marginTop="16dp"

android:text="Login"/>

</RelativeLayout>

Create the LoginActivity (LoginActivity.java):

// src/com/example/myapplication/LoginActivity.java

package com.example.myapplication;

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 LoginActivity extends AppCompatActivity {

// Hardcoded username and password

private static final String CORRECT_USERNAME = "mca";

private static final String CORRECT_PASSWORD = "android";

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

final EditText usernameEditText = findViewById(R.id.usernameEditText);

final EditText passwordEditText = findViewById(R.id.passwordEditText);

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

loginButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// Get user-entered credentials

String enteredUsername = usernameEditText.getText().toString();

String enteredPassword = passwordEditText.getText().toString();

// Check if credentials are correct

if (enteredUsername.equals(CORRECT_USERNAME) &&
enteredPassword.equals(CORRECT_PASSWORD)) {

// Successful login

showToast("Login successful");

} else {

// Incorrect credentials

showToast("Incorrect username or password");

});

private void showToast(String message) {

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

}
Slip 11 q1 B

Create registration form given below. Also perform appropriate validation.

Create the XML layout for the registration form (activity_registration.xml):

<!-- res/layout/activity_registration.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:paddingLeft="16dp"

android:paddingTop="16dp"

android:paddingRight="16dp"

android:paddingBottom="16dp"

tools:context=".RegistrationActivity">

<EditText

android:id="@+id/nameEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Name"/>

<EditText

android:id="@+id/emailEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/nameEditText"

android:layout_marginTop="8dp"

android:hint="Email"

android:inputType="textEmailAddress"/>

<EditText
android:id="@+id/passwordEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/emailEditText"

android:layout_marginTop="8dp"

android:hint="Password"

android:inputType="textPassword"/>

<EditText

android:id="@+id/mobileEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/passwordEditText"

android:layout_marginTop="8dp"

android:hint="Mobile Number"

android:inputType="phone"/>

<Button

android:id="@+id/registerButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/mobileEditText"

android:layout_marginTop="16dp"

android:text="Register"/>

</RelativeLayout>

Create the RegistrationActivity (RegistrationActivity.java):

// src/com/example/myapplication/RegistrationActivity.java

package com.example.myapplication;
import android.os.Bundle;

import android.util.Patterns;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RegistrationActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_registration);

final EditText nameEditText = findViewById(R.id.nameEditText);

final EditText emailEditText = findViewById(R.id.emailEditText);

final EditText passwordEditText = findViewById(R.id.passwordEditText);

final EditText mobileEditText = findViewById(R.id.mobileEditText);

Button registerButton = findViewById(R.id.registerButton);

registerButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// Get user-entered information

String name = nameEditText.getText().toString().trim();

String email = emailEditText.getText().toString().trim();

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

String mobile = mobileEditText.getText().toString().trim();


// Perform validation

if (name.isEmpty() || email.isEmpty() || password.isEmpty() || mobile.isEmpty()) {

showToast("All fields are required");

} else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {

showToast("Enter a valid email address");

} else if (password.length() < 6) {

showToast("Password must be at least 6 characters long");

} else if (mobile.length() != 10) {

showToast("Enter a valid 10-digit mobile number");

} else {

// Registration successful

showToast("Registration successful");

// You can add further actions like sending data to a server/database

});

private void showToast(String message) {

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

Q2

Create the UI layout (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:paddingLeft="16dp"

android:paddingTop="16dp"

android:paddingRight="16dp"

android:paddingBottom="16dp"

tools:context=".MainActivity">

<EditText

android:id="@+id/editTextTo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="To"/>

<EditText

android:id="@+id/editTextSubject"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/editTextTo"

android:layout_marginTop="8dp"

android:hint="Subject"/>

<EditText

android:id="@+id/editTextMessage"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/editTextSubject"

android:layout_marginTop="8dp"

android:hint="Message"/>

<Button

android:id="@+id/buttonAttach"

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:layout_below="@id/editTextMessage"

android:layout_marginTop="16dp"

android:text="Attach File"/>

<Button

android:id="@+id/buttonSend"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/buttonAttach"

android:layout_marginTop="16dp"

android:text="Send Email"/>

</RelativeLayout>

Implement the logic in MainActivity.java:

package com.example.emailsender;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.content.FileProvider;

import java.io.File;
import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_IMAGE_CAPTURE = 1;

private static final int PICK_FILE_REQUEST_CODE = 2;

private EditText editTextTo, editTextSubject, editTextMessage;

private Button buttonAttach, buttonSend;

private String currentPhotoPath;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editTextTo = findViewById(R.id.editTextTo);

editTextSubject = findViewById(R.id.editTextSubject);

editTextMessage = findViewById(R.id.editTextMessage);

buttonAttach = findViewById(R.id.buttonAttach);

buttonSend = findViewById(R.id.buttonSend);

buttonAttach.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// Open file picker or camera to attach a file

// For simplicity, this example uses the camera to take a photo


dispatchTakePictureIntent();

});

buttonSend.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

sendEmail();

});

private void dispatchTakePictureIntent() {

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

// Create the File where the photo should go

File photoFile = null;

try {

photoFile = createImageFile();

} catch (IOException ex) {

ex.printStackTrace();

// Continue only if the File was successfully created

if (photoFile != null) {

Uri photoURI = FileProvider.getUriForFile(this,

"com.example.android.fileprovider",

photoFile);

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

}
}

private File createImageFile() throws IOException {

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

String imageFileName = "JPEG_" + timeStamp + "_";

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

File image = File.createTempFile(

imageFileName, /* prefix */

".jpg", /* suffix */

storageDir /* directory */

);

// Save a file: path for use with ACTION_VIEW intents

currentPhotoPath = image.getAbsolutePath();

return image;

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

// Photo was successfully taken, you can handle it here if needed

} else if (requestCode == PICK_FILE_REQUEST_CODE && resultCode == RESULT_OK) {

// File was successfully picked, you can handle it here if needed

private void sendEmail() {

String to = editTextTo.getText().toString().trim();
String subject = editTextSubject.getText().toString().trim();

String message = editTextMessage.getText().toString().trim();

if (to.isEmpty() || subject.isEmpty() || message.isEmpty()) {

Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show();

return;

Intent emailIntent = new Intent(Intent.ACTION_SEND);

emailIntent.setType("message/rfc822");

emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});

emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

emailIntent.putExtra(Intent.EXTRA_TEXT, message);

// Attach the file (if any)

if (currentPhotoPath != null) {

Uri photoUri = Uri.fromFile(new File(currentPhotoPath));

emailIntent.putExtra(Intent.EXTRA_STREAM, photoUri);

try {

startActivity(Intent.createChooser(emailIntent, "Send mail..."));

} catch (android.content.ActivityNotFoundException ex) {

Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();

You might also like