0% found this document useful (0 votes)
3 views64 pages

Android Assignment

The document contains multiple Java Android programs demonstrating user interface functionalities, including a login form, registration form, and a simple calculator. Each program includes the necessary Java code and XML layout files for user input and interaction, with validation checks for empty fields and proper input formats. The calculator program specifically allows users to perform basic arithmetic operations with error handling for division by zero.

Uploaded by

udaynikam3333
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)
3 views64 pages

Android Assignment

The document contains multiple Java Android programs demonstrating user interface functionalities, including a login form, registration form, and a simple calculator. Each program includes the necessary Java code and XML layout files for user input and interaction, with validation checks for empty fields and proper input formats. The calculator program specifically allows users to perform basic arithmetic operations with error handling for division by zero.

Uploaded by

udaynikam3333
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

Assignment – 1

1. Java Android Program to demonstrate login form with validation.


[Link]:
package [Link];
import [Link];
import [Link];
[Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private EditText usernameField;
private EditText passwordField;
private Button loginButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState); // Call the parent class's onCreate method
setContentView([Link].activity_main); // Set the layout file for the activity

usernameField = findViewById([Link]);
passwordField = findViewById([Link]);
loginButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
handleLogin(); // Call method to handle login logic
}
});

private void handleLogin() {


String username = [Link]().toString().trim();
String password = [Link]().toString().trim();
if ([Link](username)) {
[Link](this, "Please enter your username",
Toast.LENGTH_SHORT).show(); // Show error message
return; // Stop further execution
}
if ([Link](password)) {
[Link](this, "Please enter your password",
Toast.LENGTH_SHORT).show(); // Show error message
return; // Stop further execution
}
demonstration purposes)
if ([Link]("admin") && [Link]("1234")) {
[Link](this, "Login successful!", Toast.LENGTH_SHORT).show(); //
Show success message
} else {
[Link](this, "Invalid username or password",
Toast.LENGTH_SHORT).show(); // Show error message
}
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- Username input field -->


<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username"
android:inputType="text"
android:layout_marginBottom="16dp"
android:minHeight="48dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_below="@id/editTextUsername"
android:layout_marginBottom="16dp"
android:minHeight="48dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/editTextPassword"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Output:
Assignment – 2
2. Java Android Program to demonstrate Registration form with
validation.
Soln:
[Link]:
package [Link];
import [Link];
import [Link];
import [Link]; // For handling view events
import [Link]; // Button class to create buttons
import [Link]; // EditText class to create input fields
import [Link]; // Toast class to display messages to the user

import [Link]; // Base class for modern


activities

public class MainActivity extends AppCompatActivity {

// Declare UI elements
private EditText nameField;
private EditText emailField;
private EditText passwordField;
private Button registerButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState); // Call the parent class's onCreate method
setContentView([Link].activity_main); // Set the layout for the activity

// Initialize UI elements
nameField = findViewById([Link]); // Input field for name
emailField = findViewById([Link]); // Input field for email
passwordField = findViewById([Link]); // Input field for password
registerButton = findViewById([Link]); // Button for registration

// Set a click listener on the register button


[Link](new [Link]() {
@Override
public void onClick(View v) {
handleRegistration(); // Call method to handle registration logic
}
});
}
// Method to handle registration logic
private void handleRegistration() {
// Get text entered in the fields
String name = [Link]().toString().trim();
String email = [Link]().toString().trim();
String password = [Link]().toString().trim();

// Check if name field is empty


if ([Link](name)) {
[Link](this, "Please enter your name", Toast.LENGTH_SHORT).show();
// Show error message
return; // Stop further execution
}

// Check if email field is empty


if ([Link](email)) {
[Link](this, "Please enter your email", Toast.LENGTH_SHORT).show();
// Show error message
return; // Stop further execution
}

// Check if password field is empty


if ([Link](password)) {
[Link](this, "Please enter your password",
Toast.LENGTH_SHORT).show(); // Show error message
return; // Stop further execution
}

// Validate email format (simple validation)


if (![Link].EMAIL_ADDRESS.matcher(email).matches()) {
[Link](this, "Please enter a valid email",
Toast.LENGTH_SHORT).show(); // Show error message
return; // Stop further execution
}

// Show success message on successful validation


[Link](this, "Registration successful!", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:inputType="text"
android:layout_marginBottom="16dp"
android:minHeight="48dp" />

<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:layout_below="@id/editTextName"
android:layout_marginBottom="16dp"
android:minHeight="48dp" />

<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_below="@id/editTextEmail"
android:layout_marginBottom="16dp"
android:minHeight="48dp" />

<Button
android:id="@+id/buttonRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_below="@id/editTextPassword"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Output:
Assignment -3
Q. Create the simple calculator and perform appropriate operation.
[Link]:
package [Link];

import [Link]; // Import necessary classes for Android app development


import [Link]; // For checking if input fields are empty
import [Link]; // For handling view events
import [Link]; // Button class to create buttons
import [Link]; // EditText class to create input fields
import [Link]; // TextView class to display results
import [Link]; // Toast class to show messages to the user

import [Link]; // Base class for modern


activities

public class MainActivity extends AppCompatActivity {

// Declare UI elements
private EditText number1Field;
private EditText number2Field;
private Button addButton;
private Button subtractButton;
private Button multiplyButton;
private Button divideButton;
private TextView resultView;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState); // Call the parent class's onCreate method
setContentView([Link].activity_main); // Set the layout for the activity

// Initialize UI elements
number1Field = findViewById([Link].editTextNumber1); // Input field for first
number
number2Field = findViewById([Link].editTextNumber2); // Input field for second
number
addButton = findViewById([Link]); // Button for addition operation
subtractButton = findViewById([Link]); // Button for subtraction
operation
multiplyButton = findViewById([Link]); // Button for multiplication
operation
divideButton = findViewById([Link]); // Button for division operation
resultView = findViewById([Link]); // TextView to display the result

// Set click listeners for buttons


[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("add"); // Call method to perform addition
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("subtract"); // Call method to perform subtraction
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("multiply"); // Call method to perform multiplication
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("divide"); // Call method to perform division
}
});
}

// Method to perform the selected operation


private void performOperation(String operation) {
// Get text from input fields
String num1Text = [Link]().toString().trim();
String num2Text = [Link]().toString().trim();

// Check if inputs are empty


if ([Link](num1Text) || [Link](num2Text)) {
[Link](this, "Please enter both numbers",
Toast.LENGTH_SHORT).show(); // Show error message
return; // Stop further execution
}

// Parse inputs to double


double num1 = [Link](num1Text);
double num2 = [Link](num2Text);
double result = 0;

// Perform the appropriate operation based on user selection


switch (operation) {
case "add":
result = num1 + num2; // Add the numbers
break;
case "subtract":
result = num1 - num2; // Subtract the numbers
break;
case "multiply":
result = num1 * num2; // Multiply the numbers
break;
case "divide":
if (num2 != 0) {
result = num1 / num2; // Divide the numbers
} else {
[Link](this, "Cannot divide by zero",
Toast.LENGTH_SHORT).show(); // Show error message
return; // Stop further execution
}
break;
}

// Display the result


[Link]("Result: " + result);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- Input field for first number -->


<EditText
android:id="@+id/editTextNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"
android:layout_marginBottom="16dp" />

<!-- Input field for second number -->


<EditText
android:id="@+id/editTextNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
android:layout_below="@id/editTextNumber1"
android:layout_marginBottom="16dp" />

<!-- Button for addition operation -->


<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_below="@id/editTextNumber2"
android:layout_marginRight="8dp"
android:layout_alignParentStart="true" />

<!-- Button for subtraction operation -->


<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_toEndOf="@id/buttonAdd"
android:layout_alignTop="@id/buttonAdd"
android:layout_marginRight="8dp" />

<!-- Button for multiplication operation -->


<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_toEndOf="@id/buttonSubtract"
android:layout_alignTop="@id/buttonSubtract"
android:layout_marginRight="8dp" />
<!-- Button for division operation -->
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_below="@id/buttonAdd"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp" />

<!-- TextView to display the result -->


<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"
android:layout_below="@id/buttonDivide"
android:layout_marginTop="16dp" />

</RelativeLayout>
Output:
Assignment – 4
Q) Java Andorid Program to Perform all arithmetic Operations using Calculators.
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class MainActivity extends AppCompatActivity {
EditText number1, number2;
Button btnAdd, btnSubtract, btnMultiply, btnDivide;
TextView resultText;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

number1 = findViewById([Link].number1);
number2 = findViewById([Link].number2);
btnAdd = findViewById([Link]);
btnSubtract = findViewById([Link]);
btnMultiply = findViewById([Link]);
btnDivide = findViewById([Link]);
resultText = findViewById([Link]);

[Link](v -> calculate('+'));


[Link](v -> calculate('-'));
[Link](v -> calculate('*'));
[Link](v -> calculate('/'));
}
private void calculate(char operator) {
String num1Str = [Link]().toString();
String num2Str = [Link]().toString();
if ([Link]() || [Link]()) {
[Link]("Please enter both numbers.");
return;
}
double num1 = [Link](num1Str);
double num2 = [Link](num2Str);
double result = 0;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
[Link]("Cannot divide by zero!");
return;
}
result = num1 / num2;
break;
}
[Link]("Result: " + result);
}
}
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="16dp">

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/btnSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="@+id/btnMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="×" />
<Button
android:id="@+id/btnDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="÷" />
</LinearLayout>
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result will appear here"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:gravity="center"/>
</LinearLayout>
Output:
Assignment -5
Q) Create an Android application which examine, that a phone number, which a user has
entered is in the given format. * Area code should be one of the following: 040, 041, 050,
0400, 044 * There should 6- 8 numbers in telephone number (+ area code).
[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EditText phoneNumberInput = findViewById([Link]);
Button validateButton = findViewById([Link]);
TextView resultView = findViewById([Link]);
[Link](view ->
String phoneNumber = [Link]().toString();

if (isValidPhoneNumber(phoneNumber)) {
[Link]("Valid phone number");
} else {
[Link]("Invalid phone number");
}
});
}

private boolean isValidPhoneNumber(String phoneNumber) {


String[] validAreaCodes = {"040", "041", "050", "0400", "044"};
boolean validAreaCode = false;
for (String areaCode : validAreaCodes) {
if ([Link](areaCode)) {
validAreaCode = true;
break;
}
}
if (!validAreaCode) {
return false;
}

for (String areaCode : validAreaCodes) {


if ([Link](areaCode)) {
phoneNumber = [Link]([Link]());
break;
}
}

return [Link]("\\d{6,8}");
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<!-- Input field for phone number -->


<EditText
android:id="@+id/editTextPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone number"
android:inputType="phone"/>

<!-- Button to trigger validation -->


<Button
android:id="@+id/buttonValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Validate"
android:layout_marginTop="16dp"/>

<!-- TextView to display validation result -->


<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="16dp"
android:textSize="16sp"/>

</LinearLayout>
Output:
Assignment- 6
Q. Write an application to accept two numbers from the user, and displays them, but
reject input if both numbers are greater than 10 and asks for two new numbers.
[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private EditText firstNumberEditText, secondNumberEditText;


private Button submitButton;
private TextView resultTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

firstNumberEditText = findViewById([Link]);
secondNumberEditText = findViewById([Link]);
submitButton = findViewById([Link]);
resultTextView = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {

String firstNumberStr = [Link]().toString().trim();


String secondNumberStr = [Link]().toString().trim();

if ([Link]() || [Link]()) {
[Link]("Please enter both numbers.");
return;
}
int firstNumber = [Link](firstNumberStr);
int secondNumber = [Link](secondNumberStr);

if (firstNumber > 10 && secondNumber > 10) {


[Link]("Both numbers cannot be greater than 10. Please enter
new numbers.");

[Link]([Link], "Both numbers are greater than 10. Try again.",


Toast.LENGTH_SHORT).show();
[Link]("");
[Link]("");
} else {

[Link]("First Number: " + firstNumber + "\nSecond Number: " +


secondNumber);
}
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- EditText for First Number -->


<EditText
android:id="@+id/firstNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="number"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:padding="10dp"/>

<!-- EditText for Second Number -->


<EditText
android:id="@+id/secondNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="number"
android:layout_below="@id/firstNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:padding="10dp"/>

<!-- Button to submit the numbers -->


<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@id/secondNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

<!-- TextView to display the result or error message -->


<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result will be displayed here."
android:textSize="18sp"
android:layout_below="@id/submitButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:padding="10dp"/>

</RelativeLayout>
Output:
Assignment - 7
Q. Create an application that allows the user to enter a number in the textbox named
“getnum‟. Check whether the number in the textbox „ getnum‟ is palindrome or not. Print
the message accordingly in the label control named lbldisplay when the user clicks on the
button „check‟.
[Link]:
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText getnum;
private Button checkButton;
private TextView lbldisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
getnum = findViewById([Link]);
checkButton = findViewById([Link]);
lbldisplay = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String input = [Link]().toString().trim();
if ([Link]()) {
[Link]([Link], "Please enter a number",
Toast.LENGTH_SHORT).show();
} else {
if (isPalindrome(input)) {
[Link]("The number is a palindrome.");
} else {
[Link]("The number is not a palindrome.");
}
}
}
});
}
private boolean isPalindrome(String number) {
String reversed = new StringBuilder(number).reverse().toString();
return [Link](reversed);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/getnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:padding="10dp"/>
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:layout_below="@id/getnum"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/lbldisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result will appear here"
android:textSize="18sp"
android:layout_below="@id/check"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:padding="10dp"/>
</RelativeLayout>
Output:
Assignment -8
[Link] using Spinner, Buttons. Write a program to draw GUI.
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Spinner spinner = findViewById([Link]);
String[] colors = {"Red", "Green", "Blue", "Yellow"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
[Link].simple_spinner_item, colors);
[Link]([Link].simple_spinner_dropdown_item);
[Link](adapter);
[Link](new [Link]() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedColor = [Link](position).toString();
[Link]([Link], "Selected: " + selectedColor,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
[Link]([Link], "No selection made",
Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select a color:"
android:textSize="18sp"
android:paddingBottom="8dp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Output:
Assignment -9
[Link] image switcher using setFactory().
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private int[] images = {[Link].image_01, [Link].image_02,
[Link].image_03};
private int currentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

imageView = findViewById([Link]);
Button btnNext = findViewById([Link]);
[Link](v -> {
currentIndex = (currentIndex + 1) % [Link];
[Link](images[currentIndex]);
});
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="500dp"
android:layout_height="300dp"
android:src="@drawable/image_01"/>
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Image"
android:layout_marginTop="20dp"/>
</LinearLayout>
Output:
Assignment - 10
[Link] an Android application, which show to the user 5-10 quiz questions. All
questions have 4 possible options and one right option exactly. Application counts and
shows to the user how many answers were right and shows the result to the user.
Soln:
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main); // Set the main activity layout
Button startQuizButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Start the QuizActivity when the button is clicked
Intent intent = new Intent([Link], [Link]);
startActivity(intent);
}
});
}
}
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class QuizActivity extends AppCompatActivity {


private TextView questionText;
private RadioGroup optionsGroup;
private Button nextButton;
private List<Question> questions;
private int currentQuestionIndex = 0;
private int score = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_quiz);
questionText = findViewById([Link]);
optionsGroup = findViewById([Link]);
nextButton = findViewById([Link]);
loadQuestions();
displayQuestion();
[Link](v -> {
int selectedOptionId = [Link]();
if (selectedOptionId != -1) {
RadioButton selectedOption = findViewById(selectedOptionId);
String selectedAnswer = [Link]().toString();
if
([Link]([Link](currentQuestionIndex).getCorrectAnswer())) {
score++;
}
currentQuestionIndex++;
if (currentQuestionIndex < [Link]()) {
displayQuestion();
} else {
showResult();
}
}
});
}
private void loadQuestions() {
questions = new ArrayList<>();
[Link](new Question("What is the capital of France?", "Paris", "Berlin",
"Madrid", "Rome", "Paris"));
[Link](new Question("Which planet is known as the Red Planet?", "Earth",
"Mars", "Jupiter", "Venus", "Mars"));
[Link](new Question("Who wrote 'Romeo and Juliet'?", "Shakespeare",
"Hemingway", "Tolkien", "Austen", "Shakespeare"));
}
private void displayQuestion() {
Question currentQuestion = [Link](currentQuestionIndex);
[Link]([Link]());
((RadioButton) [Link](0)).setText(currentQuestion.getOption1());
((RadioButton) [Link](1)).setText(currentQuestion.getOption2());
((RadioButton) [Link](2)).setText(currentQuestion.getOption3());
((RadioButton) [Link](3)).setText(currentQuestion.getOption4());
[Link](); // Clear previous selection
}
private void showResult() {
setContentView([Link].activity_result);
TextView resultText = findViewById([Link]);
[Link]("Your score: " + score + "/" + [Link]());
}
static class Question {
private final String question;
private final String option1;
private final String option2;
private final String option3;
private final String option4;
private final String correctAnswer;
public Question(String question, String option1, String option2, String option3, String
option4, String correctAnswer) {
[Link] = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
[Link] = correctAnswer;
}
public String getQuestion() {
return question;
}
public String getOption1() {
return option1;
}
public String getOption2() {
return option2;
}
public String getOption3() {
return option3;
}
public String getOption4() {
return option4;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
}
activity_main.xml:
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Quiz App!"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center" />
<Button
android:id="@+id/startQuizButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Quiz"
android:layout_marginTop="20dp" />
</LinearLayout>
activity_quiz.xml:
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/questionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question will appear here"
android:textSize="18sp"
android:layout_marginBottom="20dp" />
<RadioGroup
android:id="@+id/optionsGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 4" />
</RadioGroup>
<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_marginTop="20dp" />
</LinearLayout>
activity_result.xml
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your score: X/Y"
android:textSize="24sp"
android:gravity="center"
android:textStyle="bold" />
</LinearLayout>
Output:
Assignment -11
[Link] an Android application, where the user can enter player name and points in
one view and display it in another view.
Soln:
[Link]:
package [Link].assignment10;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText playerNameEditText;
private EditText playerPointsEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
playerNameEditText = findViewById([Link]);
playerPointsEditText = findViewById([Link]);
Button submitButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String playerName = [Link]().toString();
String playerPoints = [Link]().toString();
Intent intent = new Intent([Link], [Link]);
[Link]("PLAYER_NAME", playerName);
[Link]("PLAYER_POINTS", playerPoints);
startActivity(intent);
}
});
}
}
[Link]:
package [Link].assignment10;
import [Link];
import [Link];
import [Link];
public class DisplayActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_display);
TextView displayTextView = findViewById([Link]);
String playerName = getIntent().getStringExtra("PLAYER_NAME");
String playerPoints = getIntent().getStringExtra("PLAYER_POINTS");
[Link]("Player: " + playerName + "\nPoints: " + playerPoints);
}
activity_main.xml:
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/playerNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Player Name" />
<EditText
android:id="@+id/playerPointsEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter Player Points" />
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
activity_display.xml:
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/displayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
Output:
Assignment - 12
[Link] an Android application, the user can enter 10 students information and stored
it in file and display student information in second view and also search the particular
student information.
Soln:
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private EditText etName, etAge, etRollNumber, etSearch;
private Button btnSaveStudent, btnSearch;
private ListView lvStudents;
private ArrayList<String> studentsList = new ArrayList<>();
private ArrayAdapter<String> studentAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

etName = findViewById([Link]);
etAge = findViewById([Link]);
etRollNumber = findViewById([Link]);
etSearch = findViewById([Link]);
btnSaveStudent = findViewById([Link]);
btnSearch = findViewById([Link]);
lvStudents = findViewById([Link]);

studentAdapter = new ArrayAdapter<>(this, [Link].simple_list_item_1,


studentsList);
[Link](studentAdapter);
loadStudentsData();
[Link](new [Link]() {
@Override
public void onClick(View v) {
saveStudentData();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
searchStudent();
}
});
}
private void saveStudentData() {
String name = [Link]().toString();
String ageString = [Link]().toString();
String rollNumber = [Link]().toString();
if ([Link]() || [Link]() || [Link]()) {
[Link]([Link], "All fields are required",
Toast.LENGTH_SHORT).show();
return;
}
int age = [Link](ageString);
String studentData = name + "," + age + "," + rollNumber + "\n";
try (FileOutputStream fos = openFileOutput("[Link]", Context.MODE_APPEND)) {
[Link]([Link]());
[Link]([Link], "Student data saved",
Toast.LENGTH_SHORT).show();
// Refresh the ListView after saving data
loadStudentsData();
} catch (IOException e) {
[Link]([Link], "Error saving data", Toast.LENGTH_SHORT).show();
}
}
private void loadStudentsData() {
try (FileInputStream fis = openFileInput("[Link]")) {
byte[] buffer = new byte[[Link]()];
[Link](buffer);
String data = new String(buffer);
String[] studentsArray = [Link]("\n");

[Link]();
for (String studentInfo : studentsArray) {
if (![Link]().isEmpty()) {
[Link](studentInfo);
}
}
} catch (IOException e) {
[Link]([Link], "Error loading data", Toast.LENGTH_SHORT).show();
}

[Link]();
}
private void searchStudent() {
String searchQuery = [Link]().toString().trim();
if ([Link](searchQuery)) {
[Link](this, "Enter a roll number to search", Toast.LENGTH_SHORT).show();
return;
}
boolean found = false;
for (String student : studentsList) {
if ([Link](searchQuery)) {
[Link](this, "Found: " + student, Toast.LENGTH_LONG).show();
found = true;
break;
}
}
if (!found) {
[Link](this, "Student not found", Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- res/layout/activity_main.xml -->
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Student Name" />
<EditText
android:id="@+id/etAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number" />
<EditText
android:id="@+id/etRollNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll Number"
android:inputType="text" />
<Button
android:id="@+id/btnSaveStudent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Student" />
<EditText
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search by Roll Number" />
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search" />
<ListView
android:id="@+id/lvStudents"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Output:
Assignment - 13
[Link] an app to display the image on date wise.
Soln:
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
imageView = findViewById([Link]);
int dayOfMonth = getCurrentDay();
int imageResource = getImageForDate(dayOfMonth);
[Link](imageResource);
}
private int getCurrentDay() {
Calendar calendar = [Link]();
return [Link](Calendar.DAY_OF_MONTH);
}
private int getImageForDate(int day) {
switch (day) {
case 1: return [Link].image_01;
case 2: return [Link].image_02;
case 3: return [Link].image_03;
case 4: return [Link].image_04;
case 5: return [Link].image_05;
default: return [Link].default_image; // Default image if no match
}
}}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#ffffff">
<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter" />
</LinearLayout>
Output:
Assignment -14
[Link] Following Table: Emp (emp_no,emp_name,address,phone,salary) Dept(dept_
no,dept_name, location) Emp-Dept is related with one-many relationship. Create
application for performing the following Operation on the table 1) Add Records into
Emp and Dept table. 2) Accept Department name from User and delete employee
information which belongs to that department.
Soln:
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private EditText edtDeptName, edtEmpName, edtAddress, edtPhone, edtSalary,
edtDeptLocation;
private Button btnAddDept, btnAddEmp, btnDeleteEmp;
private DatabaseHelper dbHelper;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
edtDeptName = findViewById([Link]);
edtEmpName = findViewById([Link]);
edtAddress = findViewById([Link]);
edtPhone = findViewById([Link]);
edtSalary = findViewById([Link]);
edtDeptLocation = findViewById([Link]);
btnAddDept = findViewById([Link]);
btnAddEmp = findViewById([Link]);
btnDeleteEmp = findViewById([Link]);

dbHelper = new DatabaseHelper(this);


database = [Link]();

[Link](new [Link]() {
@Override
public void onClick(View v) {
addDepartment();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
addEmployee();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
deleteEmployeesByDepartment();
}
});
}
private void addDepartment() {
String deptName = [Link]().toString();
String deptLocation = [Link]().toString();
if ([Link]() || [Link]()) {
[Link](this, "Please enter all department details",
Toast.LENGTH_SHORT).show();
return;
}
ContentValues values = new ContentValues();
[Link]("dept_name", deptName);
[Link]("location", deptLocation);
long result = [Link]("Dept", null, values);
if (result == -1) {
[Link](this, "Failed to add department", Toast.LENGTH_SHORT).show();
} else {
[Link](this, "Department added", Toast.LENGTH_SHORT).show();
}
}
private void addEmployee() {
String empName = [Link]().toString();
String address = [Link]().toString();
String phone = [Link]().toString();
String salary = [Link]().toString();
if ([Link]() || [Link]() || [Link]() || [Link]()) {
[Link](this, "Please enter all employee details",
Toast.LENGTH_SHORT).show();
return;
}
int deptNo = getDeptNoFromName([Link]().toString());
if (deptNo == -1) {
[Link](this, "Department not found", Toast.LENGTH_SHORT).show();
return;
}
ContentValues values = new ContentValues();
[Link]("emp_name", empName);
[Link]("address", address);
[Link]("phone", phone);
[Link]("salary", salary);
[Link]("dept_no", deptNo);
long result = [Link]("Emp", null, values);
if (result == -1) {
[Link](this, "Failed to add employee", Toast.LENGTH_SHORT).show();
} else {
[Link](this, "Employee added", Toast.LENGTH_SHORT).show();
}
}
private void deleteEmployeesByDepartment() {
String deptName = [Link]().toString();
if ([Link]()) {
[Link](this, "Please enter a department name",
Toast.LENGTH_SHORT).show();
return;
}
int deptNo = getDeptNoFromName(deptName);
if (deptNo == -1) {
[Link](this, "Department not found", Toast.LENGTH_SHORT).show();
return;
}
int deletedRows = [Link]("Emp", "dept_no = ?", new
String[]{[Link](deptNo)});
if (deletedRows > 0) {
[Link](this, "Employee(s) deleted", Toast.LENGTH_SHORT).show();
} else {
[Link](this, "No employees found in this department",
Toast.LENGTH_SHORT).show();
}
}
private int getDeptNoFromName(String deptName) {
Cursor cursor = [Link]("Dept", new String[]{"dept_no"}, "dept_name = ?", new
String[]{deptName}, null, null, null);
if (cursor != null && [Link]()) {
int deptNo = [Link]([Link]("dept_no"));
[Link]();
return deptNo;
}
return -1;
}
}
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "employee_db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_EMP = "Emp";
private static final String TABLE_DEPT = "Dept";
private static final String EMP_NO = "emp_no";
private static final String EMP_NAME = "emp_name";
private static final String ADDRESS = "address";
private static final String PHONE = "phone";
private static final String SALARY = "salary";
private static final String DEPT_NO = "dept_no";
private static final String DEPT_NO_COL = "dept_no";
private static final String DEPT_NAME = "dept_name";
private static final String LOCATION = "location";
private static final String CREATE_EMP_TABLE = "CREATE TABLE " + TABLE_EMP + " (" +
EMP_NO + " INTEGER PRIMARY KEY, " +
EMP_NAME + " TEXT, " +
ADDRESS + " TEXT, " +
PHONE + " TEXT, " +
SALARY + " REAL, " +
DEPT_NO + " INTEGER, " +
"FOREIGN KEY(" + DEPT_NO + ") REFERENCES " + TABLE_DEPT + "(" + DEPT_NO_COL +
"));";
private static final String CREATE_DEPT_TABLE = "CREATE TABLE " + TABLE_DEPT + " (" +
DEPT_NO_COL + " INTEGER PRIMARY KEY, " +
DEPT_NAME + " TEXT, " +
LOCATION + " TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
[Link](CREATE_DEPT_TABLE);
[Link](CREATE_EMP_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
[Link]("DROP TABLE IF EXISTS " + TABLE_EMP);
[Link]("DROP TABLE IF EXISTS " + TABLE_DEPT);
onCreate(db);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/edtDeptName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Department Name" />
<EditText
android:id="@+id/edtDeptLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Department Location" />
<Button
android:id="@+id/btnAddDept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Department" />
<EditText
android:id="@+id/edtEmpName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Employee Name" />
<EditText
android:id="@+id/edtAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address" />
<EditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone" />
<EditText
android:id="@+id/edtSalary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Salary" />
<Button
android:id="@+id/btnAddEmp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Employee" />
<Button
android:id="@+id/btnDeleteEmp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete Employees by Department" />
</LinearLayout>
Output:

You might also like