0% found this document useful (0 votes)
6 views

Mt Slip Solution All

The document outlines two Android applications: one for displaying a splash screen and transitioning to a main activity, and another for managing a student database with CRUD operations. The first application includes XML layouts and Java code for the splash and main activities, while the second application features a database helper class for student records and a main activity for displaying and inserting student data. Additionally, there are specifications for creating a perfect number checker and a calculator application with arithmetic operations.
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)
6 views

Mt Slip Solution All

The document outlines two Android applications: one for displaying a splash screen and transitioning to a main activity, and another for managing a student database with CRUD operations. The first application includes XML layouts and Java code for the splash and main activities, while the second application features a database helper class for student records and a main activity for displaying and inserting student data. Additionally, there are specifications for creating a perfect number checker and a calculator application with arithmetic operations.
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/ 41

Q1.

Slip1
Xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center">

<ImageView
android:id="@+id/logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true" />

<TextView
android:id="@+id/app_name"
android:layout_below="@id/logo"
android:layout_centerHorizontal="true"
android:text="My Java App"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:textColor="@android:color/black" />
</RelativeLayout>

Splashactivity. Java file:

package com.example.splashdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

private static final int SPLASH_DURATION = 3000; // 3 seconds

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Delay and move to MainActivity
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish(); // Prevent returning to this screen
}
}, SPLASH_DURATION);
}
}

package com.example.splashdemo;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
android:text="Welcome to the Main Screen!"
android:textSize="20sp"
android:textColor="#000"
android:layout_centerInParent="true" />
</RelativeLayout>

Activitymain file:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.SplashDemo">

<activity android:name=".MainActivity" />


<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Q2.

package com.example.studentdb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;

import java.util.ArrayList;

public class DBHelper extends SQLiteOpenHelper {

// Database version and name


private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "studentDatabase";

// Table and column names


private static final String TABLE_STUDENT = "Student";
private static final String COLUMN_ROLL_NO = "roll_no";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_PERCENTAGE = "percentage";

// Create table SQL query


private static final String CREATE_TABLE_STUDENT =
"CREATE TABLE " + TABLE_STUDENT + "(" +
COLUMN_ROLL_NO + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_ADDRESS + " TEXT, " +
COLUMN_PERCENTAGE + " REAL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Create the table


@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STUDENT);
}

// Upgrade the database if needed (not used in this example)


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);
onCreate(db);
}

// Insert student record


public void insertStudent(String name, String address, double percentage) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_ADDRESS, address);
values.put(COLUMN_PERCENTAGE, percentage);
db.insert(TABLE_STUDENT, null, values);
db.close();
}

// Get all students' details


public ArrayList<String> getAllStudents() {
ArrayList<String> studentList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_STUDENT, null, null, null, null, null, null);


if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
int rollNo = cursor.getInt(cursor.getColumnIndex(COLUMN_ROLL_NO));
String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
String address = cursor.getString(cursor.getColumnIndex(COLUMN_ADDRESS));
double percentage = cursor.getDouble(cursor.getColumnIndex(COLUMN_PERCENTAGE));
studentList.add("Roll No: " + rollNo + ", Name: " + name + ", Address: " + address + ",
Percentage: " + percentage);
cursor.moveToNext();
}
cursor.close();
}
db.close();
return studentList;
}
}

Activitymain file:

package com.example.studentdb;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private DBHelper dbHelper;


private ListView listView;
private ArrayAdapter<String> adapter;

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

dbHelper = new DBHelper(this);


listView = findViewById(R.id.listView);
Button insertButton = findViewById(R.id.insertButton);

// Insert 5 student records into the database


insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertSampleData();
}
});

// Display all students' details when activity is created


displayAllStudents();
}

// Insert sample data of 5 students


private void insertSampleData() {
dbHelper.insertStudent("John Doe", "1234 Elm Street", 85.5);
dbHelper.insertStudent("Jane Smith", "5678 Oak Avenue", 90.2);
dbHelper.insertStudent("Alex Johnson", "91011 Pine Road", 78.3);
dbHelper.insertStudent("Emily Davis", "1213 Maple Lane", 88.7);
dbHelper.insertStudent("Michael Brown", "1415 Birch Blvd", 92.1);
}
// Display all students' details
private void displayAllStudents() {
ArrayList<String> studentList = dbHelper.getAllStudents();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, studentList);
listView.setAdapter(adapter);
}
}

Xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Button to insert sample data -->


<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert Sample Data"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<!-- ListView to display student details -->


<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/insertButton"
android:layout_marginTop="20dp" />
</RelativeLayout>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.StudentDB">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Slip 2
Q.1) Create an application that allows the user to enter a number in the textbox. Check whether the
number in the textbox is perfect number or not. Print the message using Toast control.

XML Main Activity File


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text=""
android:textSize="18sp" />

<EditText
android:id="@+id/numberInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/resultTextView"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:hint="Enter a number"
android:inputType="number" />

<Button
android:id="@+id/checkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/numberInputEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Check" />

</RelativeLayout>

Java Main File


package com.example.slip2a;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView resultTextView;


private EditText numberInputEditText;
private Button checkButton;

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

resultTextView = findViewById(R.id.resultTextView);
numberInputEditText = findViewById(R.id.numberInputEditText);
checkButton = findViewById(R.id.checkButton);

checkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String numberString = numberInputEditText.getText().toString();
if (numberString.isEmpty()) {
showToast("Please enter a number.");
return;
}

int number = Integer.parseInt(numberString);


if (isPerfectNumber(number)) {
showToast(number + " is a perfect number.");
} else {
showToast(number + " is not a perfect number.");
}
}
});
}

// Function to check whether a number is a perfect number


private boolean isPerfectNumber(int number) {
int sum = 1;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
sum += i;
if (i != number / i) {
sum += number / i;
}
}
}
return sum == number && number != 1;
}

// Function to display a Toast message


private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
Q.2) Java Android Program to perform all arithmetic Operations using
Calculator.
XML Main Activity File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@android:color/white"
android:elevation="4dp"
android:gravity="end"
android:padding="8dp"
android:textColor="@android:color/black"
android:textSize="24sp" />

<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/display"
android:rowCount="5"
android:columnCount="4"
android:padding="8dp"
android:layout_marginTop="8dp">

<!-- Number Buttons -->


<Button
android:id="@+id/btn7"
android:text="7"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn8"
android:text="8"
android:layout_column="1"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn9"
android:text="9"
android:layout_column="2"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>

<!-- Operator Buttons -->


<Button
android:id="@+id/btnAdd"
android:text="+"
android:layout_column="3"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn4"
android:text="4"
android:layout_column="0"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn5"
android:text="5"
android:layout_column="1"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn6"
android:text="6"
android:layout_column="2"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btnSubtract"
android:text="-"
android:layout_column="3"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn1"
android:text="1"
android:layout_column="0"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn2"
android:text="2"
android:layout_column="1"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>

<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:text="3" />

<Button
android:id="@+id/btnMultiply"
android:text="*"
android:layout_column="3"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btn0"
android:text="0"
android:layout_column="0"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btnDecimal"
android:text="."
android:layout_column="1"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btnEquals"
android:text="="
android:layout_column="2"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/btnDivide"
android:text="÷"
android:layout_column="3"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>

<!-- Clear Button -->


<Button
android:id="@+id/btnClear"
android:text="C"
android:layout_column="0"
android:layout_row="4"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_width="0dp"/>
</GridLayout>
</RelativeLayout>

JAVA MAIN ACTIVITY FILE


package com.mcs.mt.a16;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Stack;
public class MainActivity extends AppCompatActivity {

private TextView display;


private StringBuilder input;

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

display = findViewById(R.id.display);
input = new StringBuilder();

// Set OnClickListener for number buttons


int[] numberButtonIds = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3,
R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9};
for (int id : numberButtonIds) {
findViewById(id).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
input.append(button.getText());
display.setText(input.toString());
}
});
}

// Set OnClickListener for operator buttons


int[] operatorButtonIds = {R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply,
R.id.btnDivide};
for (int id : operatorButtonIds) {
findViewById(id).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
input.append(button.getText());
display.setText(input.toString());
}
});
}

// Set OnClickListener for equals button


findViewById(R.id.btnEquals).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
calculate();
}
});

// Set OnClickListener for clear button


findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
input.setLength(0);
display.setText("");
}
});
}

private void calculate() {


String expression = input.toString();
if (!expression.isEmpty()) {
try {
double result = evaluate(expression);
display.setText(String.valueOf(result));
input.setLength(0);
input.append(result);
} catch (ArithmeticException | IllegalArgumentException e) {
e.printStackTrace();
display.setText("Error");
input.setLength(0);
}
}
}

public double evaluate(String expression) {


Stack<Double> operands = new Stack<>();
Stack<Character> operators = new Stack<>();

for (int i = 0; i < expression.length(); i++) {


char ch = expression.charAt(i);

if (ch == ' ')


continue;

if (ch == '(') {
operators.push(ch);
} else if (Character.isDigit(ch) || ch == '.') {
StringBuilder num = new StringBuilder();
while (i < expression.length() &&
(Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
num.append(expression.charAt(i));
i++;
}
i--;
operands.push(Double.parseDouble(num.toString()));
} else if (ch == ')') {
while (operators.peek() != '(') {
double result = applyOperator(operators.pop(), operands.pop(),
operands.pop());
operands.push(result);
}
operators.pop(); // Pop '('
} else {
while (!operators.isEmpty() && precedence(ch) <=
precedence(operators.peek())) {
double result = applyOperator(operators.pop(), operands.pop(),
operands.pop());
operands.push(result);
}
operators.push(ch);
}
}

while (!operators.isEmpty()) {
double result = applyOperator(operators.pop(), operands.pop(),
operands.pop());
operands.push(result);
}

return operands.pop();
}

private static int precedence(char operator) {


if (operator == '+' || operator == '-')
return 1;
else if (operator == '*' || operator == '/' || operator == '%')
return 2;
return 0;
}

private static double applyOperator(char operator, double b, double a) {


switch (operator) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new ArithmeticException("Division by zero");
return a / b;
case '%':
if (b == 0)
throw new ArithmeticException("Modulo by zero");
return a % b;
}
throw new IllegalArgumentException("Invalid operator: " + operator);
}
}

Slip 3
Q.1) Create an application that allows the user to enter a number in the textbox. Check whether the
number in the textbox is Armstrong or not. Print the message accordingly in the label control.

XML MAIN ACTIVITY FILE


<?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"
tools:context=".MainActivity">

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

<Button
android:id="@+id/checkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/numberInputEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Check"
android:onClick="checkArmstrongNumber" />

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text=""
android:textSize="18sp" />

</RelativeLayout>
JAVA MAIN ACTIVITY FILE
package com.example.slip3a;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

// Function to check whether a number is an Armstrong number


private boolean isArmstrongNumber(int number) {
int originalNumber = number;
int sum = 0;
int numberOfDigits = String.valueOf(number).length();

while (number > 0) {


int digit = number % 10;
sum += Math.pow(digit, numberOfDigits);
number /= 10;
}

return sum == originalNumber;


}

// Function to display the result message


private void displayResult(boolean isArmstrong) {
TextView resultTextView = findViewById(R.id.resultTextView);
if (isArmstrong) {
resultTextView.setText("The number is an Armstrong number.");
} else {
resultTextView.setText("The number is not an Armstrong number.");
}
}

// Function called when the Check button is clicked


public void checkArmstrongNumber(View view) {
EditText numberInputEditText = findViewById(R.id.numberInputEditText);
String numberString = numberInputEditText.getText().toString();

if (numberString.isEmpty()) {
displayResult(false); // Empty input is not an Armstrong number
return;
}

int number = Integer.parseInt(numberString);


boolean isArmstrong = isArmstrongNumber(number);
displayResult(isArmstrong);
}
}

Q.2) Create an Android application which examine a phone number entered by a user with 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)
XML MAIN ACTIVITY FILE
<?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"
tools:context=".MainActivity">

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

<Button
android:id="@+id/checkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/phoneNumberEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Check"
android:onClick="checkPhoneNumber" />

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text=""
android:textSize="18sp" />

</RelativeLayout>

JAVA MAIN ACTIVITY FILE


package com.example.slip3b;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

// Function to check if the entered phone number is valid


private boolean isValidPhoneNumber(String phoneNumber) {
// Check if the phone number starts with one of the area codes
if (!phoneNumber.startsWith("040") &&
!phoneNumber.startsWith("041") &&
!phoneNumber.startsWith("050") &&
!phoneNumber.startsWith("0400") &&
!phoneNumber.startsWith("044")) {
return false;
}

// Remove the area code from the phone number


String phoneNumberWithoutAreaCode = phoneNumber.substring(3);

// Check if the remaining part of the phone number contains 6 to 8 digits


return phoneNumberWithoutAreaCode.length() >= 6 &&
phoneNumberWithoutAreaCode.length() <= 8;
}

// Function to display the result message


private void displayResult(boolean isValid) {
TextView resultTextView = findViewById(R.id.resultTextView);
if (isValid) {
resultTextView.setText("The phone number is valid.");
} else {
resultTextView.setText("Invalid phone number. Please enter a valid
phone number.");
}
}

// Function called when the Check button is clicked


public void checkPhoneNumber(View view) {
EditText phoneNumberEditText = findViewById(R.id.phoneNumberEditText);
String phoneNumber = phoneNumberEditText.getText().toString();

if (phoneNumber.isEmpty()) {
displayResult(false); // Empty input is not a valid phone number
return;
}

boolean isValid = isValidPhoneNumber(phoneNumber);


displayResult(isValid);
}
}

Slip 4 Q1.

Xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- ImageSwitcher to switch images -->


<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:src="@drawable/image1"/>

<!-- Button to trigger image change -->


<Button
android:id="@+id/nextImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Image"
android:layout_below="@id/imageSwitcher"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

</RelativeLayout>

Java file:
package com.example.imageswitcherapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private ImageSwitcher imageSwitcher;


private Button nextImageButton;
private int[] imageResources = {R.drawable.image1, R.drawable.image2, R.drawable.image3}; //
Add your image resources
private int currentImageIndex = 0;

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

imageSwitcher = findViewById(R.id.imageSwitcher);
nextImageButton = findViewById(R.id.nextImageButton);

// Set the factory for the ImageSwitcher


imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
// Create a new ImageView for the ImageSwitcher
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); // You can adjust this as
needed
return imageView;
}
});

// Set transition animations for the ImageSwitcher


imageSwitcher.setInAnimation(this, android.R.anim.fade_in); // Fade-in transition
imageSwitcher.setOutAnimation(this, android.R.anim.fade_out); // Fade-out transition

// Set initial image


imageSwitcher.setImageResource(imageResources[currentImageIndex]);

// Button click to switch images


nextImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Increment the index and reset if it exceeds the array size
currentImageIndex = (currentImageIndex + 1) % imageResources.length;
imageSwitcher.setImageResource(imageResources[currentImageIndex]);
}
});
}
}

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.ImagesSwitcherApp">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Slip 5
Q.1) Java Android Program to Demonstrate Alert Dialog Box.

XML MAIN ACTIVITY FILE


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="SHOW DIALOG" />

</RelativeLayout>

JAVA MAIN ACTIVITY FILE


package com.example.as18;

import android.os.Build;
import android.os.Bundle;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.button);
but.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Create an alert dialog box
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);

// Set alert title


builder.setTitle("Alert!!!");

// Set the value for the positive reaction from the user
// You can also set a listener to call when it is pressed
builder.setPositiveButton("ok", null);

// The message
builder.setMessage("welcome");

// Create the alert dialog and display it


AlertDialog theAlertDialog = builder.create();
theAlertDialog.show();

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

Q.2) Create an Android application which will ask the user to input his / her name. A message should
display the two items concatenated in a label. Change the format of the label using radio buttons and
check boxes for selection. The user can make the label text bold, underlined or italic as well as
change its color. Also include buttons to display the message in the label, clear the text boxes as well
as label. Finally exit.

XML FILE
<?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"
tools:context=".MainActivity">

<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:layout_margin="16dp"
android:inputType="text" />

<Button
android:id="@+id/displayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Message"
android:layout_below="@id/nameEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="displayMessage" />

<CheckBox
android:id="@+id/boldCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bold"
android:layout_below="@id/displayButton"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />

<CheckBox
android:id="@+id/italicCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Italic"
android:layout_below="@id/displayButton"
android:layout_toEndOf="@id/boldCheckBox"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />

<CheckBox
android:id="@+id/underlineCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Underline"
android:layout_below="@id/displayButton"
android:layout_toEndOf="@id/italicCheckBox"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />

<RadioGroup
android:id="@+id/colorRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/boldCheckBox"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true">

<RadioButton
android:id="@+id/redRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />

<RadioButton
android:id="@+id/blueRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />

<RadioButton
android:id="@+id/greenRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
</RadioGroup>

<Button
android:id="@+id/clearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_below="@id/colorRadioGroup"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:onClick="clearLabel" />

<Button
android:id="@+id/exitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:layout_below="@id/colorRadioGroup"
android:layout_marginTop="16dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:onClick="exitApp" />

<TextView
android:id="@+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@id/clearButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textColor="@android:color/black"
android:textSize="18sp" />

</RelativeLayout>
JAVA FILE
package com.example.slip5b;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText;


private TextView messageTextView;
private CheckBox boldCheckBox, italicCheckBox, underlineCheckBox;
private RadioButton redRadioButton, blueRadioButton, greenRadioButton;

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

nameEditText = findViewById(R.id.nameEditText);
messageTextView = findViewById(R.id.messageTextView);
boldCheckBox = findViewById(R.id.boldCheckBox);
italicCheckBox = findViewById(R.id.italicCheckBox);
underlineCheckBox = findViewById(R.id.underlineCheckBox);
redRadioButton = findViewById(R.id.redRadioButton);
blueRadioButton = findViewById(R.id.blueRadioButton);
greenRadioButton = findViewById(R.id.greenRadioButton);
}

// Function to display the message in the label with the selected format
public void displayMessage(View view) {
String name = nameEditText.getText().toString();

// Apply text formatting


int style = Typeface.NORMAL;
if (boldCheckBox.isChecked()) {
style |= Typeface.BOLD;
}
if (italicCheckBox.isChecked()) {
style |= Typeface.ITALIC;
}
if (underlineCheckBox.isChecked()) {
messageTextView.setPaintFlags(messageTextView.getPaintFlags() |
android.graphics.Paint.UNDERLINE_TEXT_FLAG);
} else {
messageTextView.setPaintFlags(messageTextView.getPaintFlags() &
(~android.graphics.Paint.UNDERLINE_TEXT_FLAG));
}

// Apply text color


int color = android.R.color.black;
if (redRadioButton.isChecked()) {
color = android.R.color.holo_red_dark;
} else if (blueRadioButton.isChecked()) {
color = android.R.color.holo_blue_dark;
} else if (greenRadioButton.isChecked()) {
color = android.R.color.holo_green_dark;
}

// Set the formatted text and color to the label


messageTextView.setText(name);
messageTextView.setTypeface(null, style);
messageTextView.setTextColor(getResources().getColor(color));
}

// Function to clear the text boxes and label


public void clearLabel(View view) {
nameEditText.setText("");
messageTextView.setText("");
boldCheckBox.setChecked(false);
italicCheckBox.setChecked(false);
underlineCheckBox.setChecked(false);
redRadioButton.setChecked(false);
blueRadioButton.setChecked(false);
greenRadioButton.setChecked(false);
messageTextView.setTypeface(null, Typeface.NORMAL);

messageTextView.setTextColor(getResources().getColor(android.R.color.black));
messageTextView.setPaintFlags(messageTextView.getPaintFlags() &
(~android.graphics.Paint.UNDERLINE_TEXT_FLAG));
}

// Function to exit the application


public void exitApp(View view) {
finish();
}
}

Slip 9
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 when the user clicks on the button „Check‟.

XML FILE
<?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"
tools:context=".MainActivity">

<EditText
android:id="@+id/getnum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:layout_margin="16dp"
android:inputType="number" />

<Button
android:id="@+id/checkButton"
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="16dp"
android:onClick="checkPalindrome" />

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_below="@id/checkButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />

</RelativeLayout>

JAVA FILE
package com.example.slip9a;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

// Function to check whether a number is a palindrome


private boolean isPalindrome(int number) {
int reversedNumber = 0;
int originalNumber = number;

while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}

return originalNumber == reversedNumber;


}

// Function to display the result message


private void displayResult(boolean isPalindrome) {
TextView resultTextView = findViewById(R.id.resultTextView);
if (isPalindrome) {
resultTextView.setText("The number is a palindrome.");
} else {
resultTextView.setText("The number is not a palindrome.");
}
}

// Function called when the Check button is clicked


public void checkPalindrome(View view) {
EditText getnumEditText = findViewById(R.id.getnum);
String numberString = getnumEditText.getText().toString();

if (numberString.isEmpty()) {
displayResult(false); // Empty input is not a palindrome
return;
}

int number = Integer.parseInt(numberString);


boolean isPalindrome = isPalindrome(number);
displayResult(isPalindrome);
}
}

Q.2] Java android program to create simple calculator.

ALREADY MENTION

Slip 10
Q.1] 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 Armstrong or not. Print the message using Toast
control when the user clicks on the button Check.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/ap
k/res/android" xmlns:app="http://schemas.android.com/apk/res-
auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" andr
oid:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity">

<TextView android:id="@+id/titleTextView" android:layout_width="wrap_content" android:layout_h


eight="wrap_content" android:layout_marginTop="64dp" android:text="Armstrong Number
Checker" android:textSize="24sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="par
ent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/>

<TextView android:id="@+id/instructionTextView" android:layout_width="wrap_content" android:la


yout_height="wrap_content" android:text="Enter a number to
check:" android:textSize="16sp" app:layout_constraintBottom_toTopOf="@+id/numberInput" app:la
yout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_co
nstraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/titleTextView" app:l
ayout_constraintVertical_bias="0.526"/>

<EditText android:id="@+id/numberInput" android:layout_width="200dp" android:layout_height="w


rap_content" android:ems="10" android:hint="Enter
number" android:inputType="number" app:layout_constraintBottom_toBottomOf="parent" app:layo
ut_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_const
raintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVer
tical_bias="0.266"/>

<Button android:id="@+id/checkButton" android:layout_width="wrap_content" android:layout_heig


ht="wrap_content" android:layout_marginTop="48dp" android:text="Check" app:layout_constraintE
nd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toS
tartOf="parent" app:layout_constraintTop_toBottomOf="@+id/numberInput"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<EditText android:id="@+id/itemEditText" android:layout_width="ma
tch_parent" android:layout_height="wrap_content" android:hint="A
pple" android:layout_marginBottom="16dp" android:inputType="tex
t"/>
<LinearLayout android:layout_width="match_parent" android:layout
_height="wrap_content" android:orientation="horizontal" android:la
yout_marginBottom="24dp">
<Button android:id="@+id/addButton" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_weight="1" a
ndroid:layout_marginEnd="8dp" android:text="Add to spinner"/>
<Button android:id="@+id/removeButton" android:layout_width="0
dp" android:layout_height="wrap_content" android:layout_weight="
1" android:layout_marginStart="8dp" android:text="Remove from
spinner"/>
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_hei
ght="wrap_content" android:text="See Response
Below" android:textSize="16sp" android:layout_marginBottom="8dp
"/>
<Spinner android:id="@+id/itemSpinner" android:layout_width="ma
tch_parent" android:layout_height="wrap_content" android:minHeig
ht="48dp" android:background="@android:drawable/btn_dropdown
"/>
</LinearLayout>

Slip 11
Q.1] Create an Android Application to accept two numbers to calculate its Power and Average. Create
two buttons: Power and Average. Display the appropriate result on the next activity on Button click.

<LinearLayout 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:orientation="vertical" android:padding="16dp"
tools:context=".MainActivity">

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


android:text="Number Calculator" android:textSize="24sp" android:textStyle="bold"
android:gravity="center" android:layout_marginBottom="32dp"/>

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


android:text="Enter First Number:" android:textSize="16sp"/>

<EditText android:id="@+id/editTextNumber1" android:layout_width="match_parent"


android:layout_height="wrap_content" android:layout_marginBottom="16dp"
android:inputType="numberDecimal" android:hint="First Number"/>

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


android:text="Enter Second Number:" android:textSize="16sp"/>

<EditText android:id="@+id/editTextNumber2" android:layout_width="match_parent"


android:layout_height="wrap_content" android:layout_marginBottom="32dp"
android:inputType="numberDecimal" android:hint="Second Number"/>

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


android:text="Select Operation:" android:textSize="16sp" android:layout_marginBottom="16dp"/>

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


android:orientation="horizontal">

<Button android:id="@+id/buttonPower" android:layout_width="0dp"


android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginEnd="8dp" android:text="Power"/>

<Button android:id="@+id/buttonAverage" android:layout_width="0dp"


android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginStart="8dp" android:text="Average"/>

</LinearLayout>

</LinearLayout>

Q.2] Create an Android Application to perform following string operation according to user selection
of radio button.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sc


hemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_p
arent" android:orientation="vertical" android:padding="16dp" android:background="#F5F5DC" tools
:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:te
xt="Enter
String:" android:textSize="18sp" android:textStyle="bold" android:layout_marginBottom="8dp"/>

<EditText android:id="@+id/editTextInput" android:layout_width="match_parent" android:layout_he


ight="wrap_content" android:inputType="text" android:hint="Enter text
here" android:background="@android:color/white" android:padding="8dp" android:layout_marginB
ottom="24dp"/>

<RadioGroup android:id="@+id/radioGroupOperations" android:layout_width="match_parent" andr


oid:layout_height="wrap_content" android:layout_marginBottom="24dp">

<RadioButton android:id="@+id/radioUppercase" android:layout_width="match_parent" android:lay


out_height="wrap_content" android:text="Uppercase" android:textSize="16sp"/>

<RadioButton android:id="@+id/radioLowercase" android:layout_width="match_parent" android:lay


out_height="wrap_content" android:text="Lowercase" android:textSize="16sp"/>

<RadioButton android:id="@+id/radioRight5" android:layout_width="match_parent" android:layout


_height="wrap_content" android:text="Right 5 Character" android:textSize="16sp"/>

<RadioButton android:id="@+id/radioLeft5" android:layout_width="match_parent" android:layout_


height="wrap_content" android:text="Left 5 Character" android:textSize="16sp"/>

</RadioGroup>

<Button android:id="@+id/buttonClick" android:layout_width="wrap_content" android:layout_heigh


t="wrap_content" android:text="Click" android:layout_gravity="center_horizontal" android:layout_m
arginBottom="24dp"/>

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


xt="Output:" android:textSize="18sp" android:textStyle="bold" android:layout_marginBottom="8dp"
/>

<EditText android:id="@+id/editTextOutput" android:layout_width="match_parent" android:layout_


height="wrap_content" android:inputType="text" android:enabled="false" android:background="@a
ndroid:color/white" android:padding="8dp"/>

</LinearLayout>

Slip 12
Q.1] Construct an Android app that toggles a light bulb ON and OFF when the user clicks on toggle
button.

XML FILE
<?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"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:checked="false"
android:layout_centerInParent="true"
android:onClick="toggleLight" />

</RelativeLayout>

JAVA FILE
package com.example.slip12a;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private boolean isLightOn = false;

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

ToggleButton toggleButton = findViewById(R.id.toggleButton);


toggleButton.setChecked(isLightOn);
}

public void toggleLight(View view) {


ToggleButton toggleButton = (ToggleButton) view;
isLightOn = toggleButton.isChecked();

if (isLightOn) {
// Light is turned on
// You can perform actions here to control a real light bulb
Toast.makeText(this, "Light is ON", Toast.LENGTH_SHORT).show();
} else {
// Light is turned off
// You can perform actions here to control a real light bulb
Toast.makeText(this, "Light is OFF", Toast.LENGTH_SHORT).show();
}
}
}

Q.2] Create an Android application which will ask the user to input his / her name. A message should
display the two items concatenated in a label. Change the format of the label using radio buttons and
check boxes for selection. The user can make the label text bold, underlined or italic as well as
change its color. Also include buttons to display the message in the label, clear the text boxes as well
as label. Finally exit.
ALREADY MENTION

Slip 13
Java android program to demonstrate Registration form with validation.

XML FILE
<?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"
tools:context=".MainActivity">

<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textPersonName"
android:layout_margin="16dp" />

<EditText
android:id="@+id/mobileEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mobile Number"
android:inputType="phone"
android:layout_below="@id/nameEditText"
android:layout_margin="16dp" />

<EditText
android:id="@+id/ageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"
android:layout_below="@id/mobileEditText"
android:layout_margin="16dp" />

<EditText
android:id="@+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:inputType="textPostalAddress"
android:layout_below="@id/ageEditText"
android:layout_margin="16dp" />

<Button
android:id="@+id/registerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_below="@id/addressEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:onClick="registerUser" />

</RelativeLayout>
JAVA FILE
package com.example.slip13a;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText, mobileEditText, ageEditText, addressEditText;

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

nameEditText = findViewById(R.id.nameEditText);
mobileEditText = findViewById(R.id.mobileEditText);
ageEditText = findViewById(R.id.ageEditText);
addressEditText = findViewById(R.id.addressEditText);
}

public void registerUser(View view) {


String name = nameEditText.getText().toString().trim();
String mobile = mobileEditText.getText().toString().trim();
String age = ageEditText.getText().toString().trim();
String address = addressEditText.getText().toString().trim();

if (TextUtils.isEmpty(name)) {
nameEditText.setError("Please enter your name");
return;
}

if (TextUtils.isEmpty(mobile)) {
mobileEditText.setError("Please enter your mobile number");
return;
}

if (TextUtils.isEmpty(age)) {
ageEditText.setError("Please enter your age");
return;
}

if (TextUtils.isEmpty(address)) {
addressEditText.setError("Please enter your address");
return;
}

// Perform further actions (e.g., save data to database) if all fields are
valid
// For demonstration, we display a toast message
Toast.makeText(this, "User registered successfully",
Toast.LENGTH_SHORT).show();
}
}

Q.2] Write a Java Android Program to Demonstrate List View Activity with all operations Such as:
Insert, Delete, Search
Slip 14
Q.1] Construct an Android application to accept a number and calculate and display Factorial of a
given number in TextView.

XML FILE
<?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"
tools:context=".MainActivity">

<EditText
android:id="@+id/numberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number"
android:layout_margin="16dp"/>

<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Factorial"
android:layout_below="@id/numberEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="calculateFactorial"/>

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_below="@id/calculateButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />

</RelativeLayout>

JAVA FILE
package com.example.slip14a;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText numberEditText;


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

numberEditText = findViewById(R.id.numberEditText);
resultTextView = findViewById(R.id.resultTextView);
}

public void calculateFactorial(View view) {


String inputStr = numberEditText.getText().toString().trim();

if (!inputStr.isEmpty()) {
int number = Integer.parseInt(inputStr);
long factorial = calculateFactorial(number);
resultTextView.setText("Factorial of " + number + " is: " + factorial);
} else {
resultTextView.setText("Please enter a number.");
}
}

private long calculateFactorial(int n) {


if (n == 0 || n == 1) {
return 1;
}
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}

Q.2] Create an Android application, which show Login Form. After clicking LOGIN button display the
“Login Successful…” message if username and password is same else display “Invalid Login” message
in Toast Control.

XML FILE
<?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"
tools:context=".MainActivity">

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_margin="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_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/passwordEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="login"/>

</RelativeLayout>

JAVA FILE
package com.example.slip14b;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText usernameEditText;


private EditText passwordEditText;

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

usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
}

public void login(View view) {


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

if (username.equals(password)) {
Toast.makeText(this, "Login Successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid Login", Toast.LENGTH_SHORT).show();
}
}
}

slip 19
<RelativeLayout xmlns:android="http://schemas.android.com/apk/re
s/android" xmlns:tools="http://schemas.android.com/tools" android:
layout_width="match_parent" android:layout_height="match_paren
t" android:padding="16dp" tools:context=".MainActivity">
<ImageSwitcher android:id="@+id/imageSwitcher" android:layout_w
idth="385dp" android:layout_height="574dp" android:layout_above=
"@+id/buttonLayout" android:layout_marginBottom="77dp"/>
<LinearLayout android:id="@+id/buttonLayout" android:layout_widt
h="match_parent" android:layout_height="wrap_content" android:la
yout_alignParentBottom="true" android:layout_marginBottom="100
dp" android:gravity="center" android:orientation="horizontal">
<Button android:id="@+id/prevButton" android:layout_width="wrap
_content" android:layout_height="wrap_content" android:layout_ma
rginEnd="16dp" android:text="Previous"/>
<Button android:id="@+id/nextButton" android:layout_width="wrap
_content" android:layout_height="wrap_content" android:layout_ma
rginStart="16dp" android:text="Next"/>
</LinearLayout>
</RelativeLayout>
package slip-4;

public package com.example.imageswitcher;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {


private ImageSwitcher imageSwitcher;
private Button nextButton, prevButton;

private int[] images = {


R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6
};

private int currentIndex = 0;

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

imageSwitcher = findViewById(R.id.imageSwitcher);

// Set the factory to create ImageView for ImageSwitcher


imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {

ImageView imageView = new


ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new
ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return imageView;
}
});

imageSwitcher.setImageResource(images[currentIndex]);

nextButton = findViewById(R.id.nextButton);
prevButton = findViewById(R.id.prevButton);

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

currentIndex++;
if (currentIndex >= images.length) {
currentIndex = 0;
}
imageSwitcher.setImageResource(images[currentIndex]);
}
});

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

currentIndex--;
if (currentIndex < 0) {
currentIndex = images.length - 1;
}
imageSwitcher.setImageResource(images[currentIndex]);
}
});
}
}{

}
Slip 25
Q.1] Create an android application for SMS activity.

XML FILE
<?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"
tools:context=".MainActivity">

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

<EditText
android:id="@+id/messageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter message"
android:layout_below="@id/phoneNumberEditText"
android:layout_margin="16dp"/>

<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:layout_below="@id/messageEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="sendSMS"/>

</RelativeLayout>

JAVA FILE
package com.example.slip25a;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText phoneNumberEditText;


private EditText messageEditText;

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

phoneNumberEditText = findViewById(R.id.phoneNumberEditText);
messageEditText = findViewById(R.id.messageEditText);
}

public void sendSMS(View view) {


String phoneNumber = phoneNumberEditText.getText().toString();
String message = messageEditText.getText().toString();

Uri uri = Uri.parse("smsto:" + phoneNumber);


Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", message);
startActivity(intent);
}
}

Q.2] Create an Android application, which show Login Form in table layout. After clicking LOGIN
button display the “Login Successful…” message if username and password is same else display
“Invalid Login” message in Toast Control.

ALREADY MENTION

You might also like