1. Write an application to create a splash screen.
import [Link];
import [Link];
import [Link];
import [Link];
public class SplashActivity extends AppCompatActivity {
// Set the splash screen display time in milliseconds (3 seconds in this case)
private static final int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_splash);
// Using a Handler to delay the transition to the main activity
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Start the MainActivity after the delay
Intent intent = new Intent([Link], [Link]);
startActivity(intent);
finish(); // Finish the SplashActivity so it can't be returned to
}
}, SPLASH_TIME_OUT);
}
}
2. Create table Student (roll no, name, address, percentage). Create Application for
performing the following operation on the table. (Using SQLite database). i] Insert
record of 5 new student details. ii] Show all the student details.
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DBHelper extends SQLiteOpenHelper {
// Database name and version
private static final String DATABASE_NAME = "StudentDB";
private static final int DATABASE_VERSION = 1;
// Table name and columns
private static final String TABLE_NAME = "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";
// SQL to create the table
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
+ "("
+ COLUMN_ROLL_NO + " INTEGER PRIMARY KEY, "
+ COLUMN_NAME + " TEXT, "
+ COLUMN_ADDRESS + " TEXT, "
+ COLUMN_PERCENTAGE + " REAL);";
// Constructor
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create the table when the database is first created
[Link](CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop the old table if it exists and create a new one
[Link]("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
// Insert a new student record into the database
public void insertStudent(int rollNo, String name, String address, double percentage) {
SQLiteDatabase db = [Link]();
ContentValues values = new ContentValues();
[Link](COLUMN_ROLL_NO, rollNo);
[Link](COLUMN_NAME, name);
[Link](COLUMN_ADDRESS, address);
[Link](COLUMN_PERCENTAGE, percentage);
[Link](TABLE_NAME, null, values);
[Link]();
}
// Get all student details from the database
public ArrayList<Student> getAllStudents() {
ArrayList<Student> students = new ArrayList<>();
SQLiteDatabase db = [Link]();
// Query all rows
Cursor cursor = [Link]("SELECT * FROM " + TABLE_NAME, null);
// Loop through the results
if ([Link]()) {
do {
int rollNo = [Link]([Link](COLUMN_ROLL_NO));
String name = [Link]([Link](COLUMN_NAME));
String address =
[Link]([Link](COLUMN_ADDRESS));
double percentage =
[Link]([Link](COLUMN_PERCENTAGE));
[Link](new Student(rollNo, name, address, percentage));
} while ([Link]());
}
[Link]();
[Link]();
return students;
}
}
[Link]
public class Student {
private int rollNo;
private String name;
private String address;
private double percentage;
public Student(int rollNo, String name, String address, double percentage) {
[Link] = rollNo;
[Link] = name;
[Link] = address;
[Link] = percentage;
}
public int getRollNo() {
return rollNo;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public double getPercentage() {
return percentage;
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private DBHelper dbHelper;
private StudentAdapter studentAdapter;
private ListView listView;
private Button insertButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
dbHelper = new DBHelper(this);
listView = findViewById([Link]);
insertButton = findViewById([Link]);
// Insert records of 5 students
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Insert 5 student records
[Link](1, "John Doe", "1234 Elm Street", 85.5);
[Link](2, "Jane Smith", "5678 Oak Street", 90.0);
[Link](3, "Sam Brown", "1234 Pine Street", 78.3);
[Link](4, "Lucy Green", "9101 Maple Avenue", 92.5);
[Link](5, "Mark White", "1234 Cedar Drive", 88.0);
[Link]([Link], "Inserted 5 Students",
Toast.LENGTH_SHORT).show();
// Refresh the list view after insertion
loadStudentData();
}
});
// Load and display all students
loadStudentData();
}
// Load student data and display it in a ListView
private void loadStudentData() {
ArrayList<Student> students = [Link]();
studentAdapter = new StudentAdapter(this, students);
[Link](studentAdapter);
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class StudentAdapter extends ArrayAdapter<Student> {
private Context context;
private ArrayList<Student> students;
public StudentAdapter(Context context, ArrayList<Student> students) {
super(context, 0, students);
[Link] = context;
[Link] = students;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = [Link](context).inflate([Link].student_item, parent,
false);
}
// Get the current student item
Student student = [Link](position);
// Set the student details in the ListView item
TextView rollNoText = [Link]([Link]);
TextView nameText = [Link]([Link]);
TextView addressText = [Link]([Link]);
TextView percentageText = [Link]([Link]);
[Link]([Link]([Link]()));
[Link]([Link]());
[Link]([Link]());
[Link]([Link]([Link]()) + "%");
return convertView;
}
}
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">
<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert 5 Students" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
1. Construct an Android application to accept a number and calculate Armstrong and
Perfect number of a given number.
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="16dp">
<!-- EditText for entering the number -->
<EditText
android:id="@+id/numberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="numberDecimal"/>
<!-- Button to check Armstrong number -->
<Button
android:id="@+id/armstrongButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Armstrong Number"
android:layout_marginTop="20dp"/>
<!-- Button to check Perfect number -->
<Button
android:id="@+id/perfectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Perfect Number"
android:layout_marginTop="20dp"/>
<!-- TextView to display the result -->
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result will appear here"
android:textSize="18sp"
android:layout_marginTop="30dp"/>
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText numberEditText;
private Button armstrongButton, perfectButton;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize views
numberEditText = findViewById([Link]);
armstrongButton = findViewById([Link]);
perfectButton = findViewById([Link]);
resultTextView = findViewById([Link]);
// Set click listener for Armstrong number check
[Link](new [Link]() {
@Override
public void onClick(View v) {
String numberStr = [Link]().toString();
if ([Link]()) {
[Link]("Please enter a number.");
return;
}
int number = [Link](numberStr);
if (isArmstrongNumber(number)) {
[Link](number + " is an Armstrong number.");
} else {
[Link](number + " is not an Armstrong number.");
}
}
});
// Set click listener for Perfect number check
[Link](new [Link]() {
@Override
public void onClick(View v) {
String numberStr = [Link]().toString();
if ([Link]()) {
[Link]("Please enter a number.");
return;
}
int number = [Link](numberStr);
if (isPerfectNumber(number)) {
[Link](number + " is a Perfect number.");
} else {
[Link](number + " is not a Perfect number.");
}
}
});
}
// Method to check if a number is Armstrong
private boolean isArmstrongNumber(int number) {
int sum = 0, temp, remainder;
int digits = [Link](number).length();
temp = number;
while (temp != 0) {
remainder = temp % 10;
sum += [Link](remainder, digits);
temp /= 10;
}
return sum == number;
}
// Method to check if a number is Perfect
private boolean isPerfectNumber(int number) {
int sum = 0;
for (int i = 1; i <= number / 2; i++) {
if (number % i == 0) {
sum += i;
}
}
return sum == number;
}
}
Construct image switcher using setFactory().
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="16dp">
<!-- ImageSwitcher to display images -->
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right" />
<!-- Button to switch images -->
<Button
android:id="@+id/nextImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Image"
android:layout_marginTop="20dp" />
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
// Define the ImageSwitcher and button
private ImageSwitcher imageSwitcher;
private Button nextImageButton;
// Array of images to be displayed in the ImageSwitcher
private int[] imageIds = {[Link].image1, [Link].image2,
[Link].image3};
private int currentIndex = 0; // To track the current image being displayed
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize ImageSwitcher and Button
imageSwitcher = findViewById([Link]);
nextImageButton = findViewById([Link]);
// Set the ImageSwitcher Factory
[Link](() -> {
ImageView imageView = new ImageView([Link]);
[Link]([Link].CENTER_CROP);
return imageView;
});
// Set the first image
[Link](imageIds[currentIndex]);
// Set an onClickListener for the button to switch images
[Link](v -> {
// Increment the index to get the next image
currentIndex = (currentIndex + 1) % [Link]; // Wrap around when it
reaches the end
// Set the next image
[Link](imageIds[currentIndex]);
// Optionally, display a toast with the index of the current image
[Link]([Link], "Image " + (currentIndex + 1),
Toast.LENGTH_SHORT).show();
});
}
}
1. Construct an Android application to accept a number and calculate Armstrong and
Perfect number of a given number.
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="16dp">
<!-- EditText for entering the number -->
<EditText
android:id="@+id/numberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="numberDecimal"/>
<!-- Button to check Armstrong number -->
<Button
android:id="@+id/armstrongButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Armstrong Number"
android:layout_marginTop="20dp"/>
<!-- Button to check Perfect number -->
<Button
android:id="@+id/perfectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Perfect Number"
android:layout_marginTop="20dp"/>
<!-- TextView to display the result -->
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result will appear here"
android:textSize="18sp"
android:layout_marginTop="30dp"/>
</LinearLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText numberEditText;
private Button armstrongButton, perfectButton;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize views
numberEditText = findViewById([Link]);
armstrongButton = findViewById([Link]);
perfectButton = findViewById([Link]);
resultTextView = findViewById([Link]);
// Set click listener for Armstrong number check
[Link](new [Link]() {
@Override
public void onClick(View v) {
String numberStr = [Link]().toString();
if ([Link]()) {
[Link]("Please enter a number.");
return;
}
int number = [Link](numberStr);
if (isArmstrongNumber(number)) {
[Link](number + " is an Armstrong number.");
} else {
[Link](number + " is not an Armstrong number.");
}
}
});
// Set click listener for Perfect number check
[Link](new [Link]() {
@Override
public void onClick(View v) {
String numberStr = [Link]().toString();
if ([Link]()) {
[Link]("Please enter a number.");
return;
}
int number = [Link](numberStr);
if (isPerfectNumber(number)) {
[Link](number + " is a Perfect number.");
} else {
[Link](number + " is not a Perfect number.");
}
}
});
}
// Method to check if a number is Armstrong
private boolean isArmstrongNumber(int number) {
int sum = 0, temp, remainder;
int digits = [Link](number).length();
temp = number;
while (temp != 0) {
remainder = temp % 10;
sum += [Link](remainder, digits);
temp /= 10;
}
return sum == number;
}
// Method to check if a number is Perfect
private boolean isPerfectNumber(int number) {
int sum = 0;
for (int i = 1; i <= number / 2; i++) {
if (number % i == 0) {
sum += i;
}
}
return sum == number;
}
}
Create an Android Application that will change color of the screen
and change the font size of text view using xml.
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"
android:gravity="center">
<!-- EditText to input the string -->
<EditText
android:id="@+id/inputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text here"
android:textSize="18sp"/>
<!-- TextView to display the string with the selected font settings -->
<TextView
android:id="@+id/displayText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is sample text"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:gravity="center"/>
<!-- SeekBar to adjust font size -->
<SeekBar
android:id="@+id/fontSizeSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="18"
android:layout_marginTop="20dp"/>
<!-- Spinner to select font family -->
<Spinner
android:id="@+id/fontFamilySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
<!-- Button to pick font color -->
<Button
android:id="@+id/colorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Color"
android:layout_marginTop="20dp"/>
</LinearLayout>
[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];
public class MainActivity extends AppCompatActivity {
private EditText inputText;
private TextView displayText;
private SeekBar fontSizeSeekBar;
private Spinner fontFamilySpinner;
private Button colorButton;
private String[] fontFamilies = {"Default", "Serif", "Monospace", "sans-serif",
"sans-serif-light"};
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize views
inputText = findViewById([Link]);
displayText = findViewById([Link]);
fontSizeSeekBar = findViewById([Link]);
fontFamilySpinner = findViewById([Link]);
colorButton = findViewById([Link]);
// Set up the font family spinner
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
[Link].simple_spinner_item, fontFamilies);
[Link]([Link].simple_spinner_dropdown_item);
[Link](adapter);
// Set the initial font size from the SeekBar
int initialFontSize = [Link]();
[Link](initialFontSize);
// Font size adjustment using SeekBar
[Link](new
[Link]() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
[Link](progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
// Change font family based on the selection in Spinner
[Link]((parentView, selectedItemView,
position, id) -> {
String selectedFont = fontFamilies[position];
switch (selectedFont) {
case "Serif":
[Link]([Link]);
break;
case "Monospace":
[Link]([Link]);
break;
case "sans-serif":
[Link]([Link].SANS_SERIF);
break;
case "sans-serif-light":
[Link]([Link]("sans-serif-light",
[Link]));
break;
default:
[Link]([Link]);
break;
}
});
// Open color picker when button is clicked
[Link](v -> {
[Link]()
.setDialogId(0)
.setAllowCustom(true)
.setShowAlphaSlider(true)
.setColor([Link])
.setPresets(new int[] {[Link], [Link], [Link],
[Link]})
.setCallback(new OnColorSelectedListener() {
@Override
public void onColorSelected(int color) {
[Link](color);
}
})
.build()
.show([Link]);
});
}
}
1. Create an application for registration form given below. Also perform appropriate
validation.
<ScrollView xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textPersonName" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/etConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone" />
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:layout_marginTop="16dp"/>
</LinearLayout>
</ScrollView>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText etUsername, etEmail, etPassword, etConfirmPassword, etPhone;
private Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_registration);
etUsername = findViewById([Link]);
etEmail = findViewById([Link]);
etPassword = findViewById([Link]);
etConfirmPassword = findViewById([Link]);
etPhone = findViewById([Link]);
btnRegister = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if (validateInputs()) {
[Link]([Link], "Registration Successful",
Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean validateInputs() {
// Username Validation
if ([Link]([Link]())) {
[Link]("Username is required");
[Link]();
return false;
}
// Email Validation
if ([Link]([Link]()) ||
!Patterns.EMAIL_ADDRESS.matcher([Link]()).matches()) {
[Link]("Valid email is required");
[Link]();
return false;
}
// Password Validation
if ([Link]([Link]()) || [Link]().length() < 6) {
[Link]("Password must be at least 6 characters");
[Link]();
return false;
}
// Confirm Password Validation
if (![Link]().toString().equals([Link]().toString()))
{
[Link]("Passwords do not match");
[Link]();
return false;
}
// Phone Number Validation
if ([Link]([Link]()) ||
).matches()) {
[Link]("Valid phone number is required");
[Link]();
return false;
}
return true;
}
}
Write a Java Android Program to Demonstrate Listview Activity
with all operations Such as: Insert, Delete, Search.
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"
android:gravity="center">
<!-- EditText to input item -->
<EditText
android:id="@+id/itemEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Item"
android:inputType="text"/>
<!-- Button to insert item into the list -->
<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert"
android:layout_marginTop="10dp"/>
<!-- Button to delete item from the list -->
<Button
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:layout_marginTop="10dp"/>
<!-- Button to search item in the list -->
<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_marginTop="10dp"/>
<!-- ListView to display the list of items -->
<ListView
android:id="@+id/itemListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
</LinearLayout>
[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 itemEditText;
private Button insertButton, deleteButton, searchButton;
private ListView itemListView;
private ArrayList<String> itemList;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize views
itemEditText = findViewById([Link]);
insertButton = findViewById([Link]);
deleteButton = findViewById([Link]);
searchButton = findViewById([Link]);
itemListView = findViewById([Link]);
// Initialize the list and adapter
itemList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, [Link].simple_list_item_1, itemList);
[Link](adapter);
// Insert item
[Link](new [Link]() {
@Override
public void onClick(View v) {
String item = [Link]().toString().trim();
if (![Link]()) {
[Link](item);
[Link]();
[Link](""); // Clear the input field
[Link]([Link], "Item inserted",
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Please enter an item",
Toast.LENGTH_SHORT).show();
}
}
});
// Delete item
[Link](new [Link]() {
@Override
public void onClick(View v) {
String item = [Link]().toString().trim();
if (![Link]()) {
if ([Link](item)) {
[Link](item);
[Link]();
[Link](""); // Clear the input field
[Link]([Link], "Item deleted",
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Item not found",
Toast.LENGTH_SHORT).show();
}
} else {
[Link]([Link], "Please enter an item",
Toast.LENGTH_SHORT).show();
}
}
});
// Search item
[Link](new [Link]() {
@Override
public void onClick(View v) {
String item = [Link]().toString().trim();
if (![Link]()) {
if ([Link](item)) {
[Link]([Link], "Item found: " + item,
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Item not found",
Toast.LENGTH_SHORT).show();
}
} else {
[Link]([Link], "Please enter an item",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Construct an Android application to accept a number and
calculate and display Factorial of a given number in
TextView.
activity_main.xml (UI Layout)
<?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/numberInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number" />
<Button
android:id="@+id/calculateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate Factorial" />
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingTop="10dp" />
</LinearLayout>
[Link] (Logic Implementation)
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText numberInput;
Button calculateButton;
TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
numberInput = findViewById([Link]);
calculateButton = findViewById([Link]);
resultText = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String inputText = [Link]().toString();
if ([Link]()) {
[Link]("Please enter a number.");
return;
}
int number = [Link](inputText);
if (number < 0) {
[Link]("Factorial of negative numbers is not defined.");
} else {
BigInteger factorialResult = calculateFactorial(number);
[Link]("Factorial: " + factorialResult);
}
}
});
}
private BigInteger calculateFactorial(int num) {
BigInteger factorial = [Link];
for (int i = 1; i <= num; i++) {
factorial = [Link]([Link](i));
}
return factorial;
}
}