Android Assignment
Android Assignment
@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
}
});
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
// 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
<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];
// 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
[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
}
});
}
</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]);
<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];
@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");
}
});
}
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">
</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];
@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) {
if ([Link]() || [Link]()) {
[Link]("Please enter both numbers.");
return;
}
int firstNumber = [Link](firstNumberStr);
int secondNumber = [Link](secondNumberStr);
</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];
@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];
etName = findViewById([Link]);
etAge = findViewById([Link]);
etRollNumber = findViewById([Link]);
etSearch = findViewById([Link]);
btnSaveStudent = findViewById([Link]);
btnSearch = findViewById([Link]);
lvStudents = findViewById([Link]);
[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];
[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: