Q-4: Attempt the following (Any two) (10 Marks)
Questionsโ
A: Describe the Android architecture in detail.
B: Develop a simple calculator using table layout.
C: Write java file for event handling of Checkbox in android application.
Answer A: Android Architecture in Detailโ
Android architecture is based on Linux kernel and consists of multiple layers. Here's the detailed architecture:
Android Architecture Layers (Bottom to Top):โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Applications Layer โ
โ (Phone, SMS, Calendar, Maps, Browser, etc.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Application Framework Layer โ
โ (Activity Manager, Content Providers, etc.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Libraries Layer โ Android Runtime
โ (Media, Graphics, SQLite, etc.) โ (Core Libraries + DVM)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Linux Kernel โ
โ (Drivers, Memory Management, Security) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1. Linux Kernel Layer (Bottom Layer)โ
- Foundation: Android is built on Linux kernel (version 2.6)
- Functions:
- Memory management
- Process management
- Security model
- Hardware abstraction
- Device drivers (Display, Camera, Bluetooth, Audio, etc.)
- Power management
2. Libraries Layerโ
Native Libraries (C/C++):
- Media Libraries: For audio/video playback and recording
- Surface Manager: Manages access to display subsystem
- LibWebCore: Web browser engine (WebKit)
- SGL (Scalable Graphics Library): 2D graphics engine
- 3D Libraries: OpenGL ES for 3D graphics
- FreeType: Font rendering
- SQLite: Lightweight database engine
- SSL: Secure Socket Layer for internet security
3. Android Runtimeโ
- Core Libraries: Provide functionality of Java programming language
- Dalvik Virtual Machine (DVM):
- Register-based virtual machine
- Optimized for mobile devices
- Runs .dex (Dalvik Executable) files
- Multiple instances can run simultaneously
4. Application Framework Layerโ
Key Components:
- Activity Manager: Manages activity lifecycle
- Content Providers: Manages data sharing between applications
- Resource Manager: Manages non-code resources (strings, graphics)
- Notification Manager: Manages notifications
- Location Manager: Provides location services
- Package Manager: Manages application packages
- Telephony Manager: Manages telephony services
- Window Manager: Manages windows and screens
5. Applications Layer (Top Layer)โ
- Pre-installed Apps: Phone, SMS, Calendar, Maps, Browser
- Third-party Apps: Downloaded from Play Store
- User Apps: Custom developed applications
Architecture Benefits:โ
- Reusability: Applications can reuse components
- Security: Each layer provides security
- Flexibility: Easy to develop and deploy applications
- Open Source: Customizable and extensible
Answer B: Simple Calculator using Table Layoutโ
XML Layout File (activity_calculator.xml):โ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- Display Screen -->
<EditText
android:id="@+id/editTextDisplay"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textSize="24sp"
android:gravity="right|center_vertical"
android:background="@android:color/white"
android:textColor="@android:color/black"
android:layout_marginBottom="16dp"
android:inputType="none"
android:focusable="false" />
<!-- Calculator Buttons using TableLayout -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2,3">
<!-- Row 1: Clear, +/-, %, รท -->
<TableRow android:layout_marginBottom="8dp">
<Button android:id="@+id/btnClear" android:text="C"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btnPlusMinus" android:text="+/-"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btnPercent" android:text="%"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btnDivide" android:text="รท"
android:layout_margin="4dp" android:textSize="20sp"/>
</TableRow>
<!-- Row 2: 7, 8, 9, ร -->
<TableRow android:layout_marginBottom="8dp">
<Button android:id="@+id/btn7" android:text="7"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btn8" android:text="8"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btn9" android:text="9"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btnMultiply" android:text="ร"
android:layout_margin="4dp" android:textSize="20sp"/>
</TableRow>
<!-- Row 3: 4, 5, 6, - -->
<TableRow android:layout_marginBottom="8dp">
<Button android:id="@+id/btn4" android:text="4"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btn5" android:text="5"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btn6" android:text="6"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btnSubtract" android:text="-"
android:layout_margin="4dp" android:textSize="20sp"/>
</TableRow>
<!-- Row 4: 1, 2, 3, + -->
<TableRow android:layout_marginBottom="8dp">
<Button android:id="@+id/btn1" android:text="1"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btn2" android:text="2"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btn3" android:text="3"
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btnAdd" android:text="+"
android:layout_margin="4dp" android:textSize="20sp"/>
</TableRow>
<!-- Row 5: 0 (spans 2 columns), ., = -->
<TableRow>
<Button android:id="@+id/btn0" android:text="0"
android:layout_margin="4dp" android:textSize="20sp"
android:layout_span="2"/>
<Button android:id="@+id/btnDecimal" android:text="."
android:layout_margin="4dp" android:textSize="20sp"/>
<Button android:id="@+id/btnEquals" android:text="="
android:layout_margin="4dp" android:textSize="20sp"/>
</TableRow>
</TableLayout>
</LinearLayout>
Java Activity File (CalculatorActivity.java):โ
public class CalculatorActivity extends AppCompatActivity {
private EditText display;
private String currentNumber = "";
private String operator = "";
private double firstNumber = 0;
private boolean isNewOperation = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
display = findViewById(R.id.editTextDisplay);
setupButtons();
}
private void setupButtons() {
// Number buttons
findViewById(R.id.btn0).setOnClickListener(v -> appendNumber("0"));
findViewById(R.id.btn1).setOnClickListener(v -> appendNumber("1"));
findViewById(R.id.btn2).setOnClickListener(v -> appendNumber("2"));
findViewById(R.id.btn3).setOnClickListener(v -> appendNumber("3"));
findViewById(R.id.btn4).setOnClickListener(v -> appendNumber("4"));
findViewById(R.id.btn5).setOnClickListener(v -> appendNumber("5"));
findViewById(R.id.btn6).setOnClickListener(v -> appendNumber("6"));
findViewById(R.id.btn7).setOnClickListener(v -> appendNumber("7"));
findViewById(R.id.btn8).setOnClickListener(v -> appendNumber("8"));
findViewById(R.id.btn9).setOnClickListener(v -> appendNumber("9"));
// Operator buttons
findViewById(R.id.btnAdd).setOnClickListener(v -> setOperator("+"));
findViewById(R.id.btnSubtract).setOnClickListener(v -> setOperator("-"));
findViewById(R.id.btnMultiply).setOnClickListener(v -> setOperator("ร"));
findViewById(R.id.btnDivide).setOnClickListener(v -> setOperator("รท"));
// Special buttons
findViewById(R.id.btnEquals).setOnClickListener(v -> calculate());
findViewById(R.id.btnClear).setOnClickListener(v -> clear());
findViewById(R.id.btnDecimal).setOnClickListener(v -> appendDecimal());
}
private void appendNumber(String number) {
if (isNewOperation) {
currentNumber = number;
isNewOperation = false;
} else {
currentNumber += number;
}
display.setText(currentNumber);
}
private void setOperator(String op) {
if (!currentNumber.isEmpty()) {
firstNumber = Double.parseDouble(currentNumber);
operator = op;
isNewOperation = true;
}
}
private void calculate() {
if (!operator.isEmpty() && !currentNumber.isEmpty()) {
double secondNumber = Double.parseDouble(currentNumber);
double result = 0;
switch (operator) {
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "ร":
result = firstNumber * secondNumber;
break;
case "รท":
if (secondNumber != 0) {
result = firstNumber / secondNumber;
} else {
display.setText("Error");
return;
}
break;
}
display.setText(String.valueOf(result));
currentNumber = String.valueOf(result);
operator = "";
isNewOperation = true;
}
}
private void clear() {
currentNumber = "";
operator = "";
firstNumber = 0;
isNewOperation = true;
display.setText("0");
}
private void appendDecimal() {
if (!currentNumber.contains(".")) {
if (currentNumber.isEmpty()) {
currentNumber = "0.";
} else {
currentNumber += ".";
}
display.setText(currentNumber);
}
}
}
Answer C: Java file for Checkbox Event Handlingโ
XML Layout File (activity_checkbox.xml):โ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your hobbies:"
android:textSize="18sp"
android:layout_marginBottom="20dp" />
<CheckBox
android:id="@+id/checkboxReading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading"
android:textSize="16sp"
android:layout_marginBottom="10dp" />
<CheckBox
android:id="@+id/checkboxMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listening to Music"
android:textSize="16sp"
android:layout_marginBottom="10dp" />
<CheckBox
android:id="@+id/checkboxSports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Playing Sports"
android:textSize="16sp"
android:layout_marginBottom="10dp" />
<CheckBox
android:id="@+id/checkboxTraveling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Traveling"
android:textSize="16sp"
android:layout_marginBottom="20dp" />
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="16sp"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Selected hobbies will appear here"
android:textSize="16sp"
android:background="#f0f0f0"
android:padding="10dp" />
</LinearLayout>
Java Activity File (CheckboxActivity.java):โ
package com.example.checkboxdemo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class CheckboxActivity extends AppCompatActivity {
private CheckBox checkboxReading, checkboxMusic, checkboxSports, checkboxTraveling;
private Button buttonSubmit;
private TextView textViewResult;
private ArrayList<String> selectedHobbies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
// Initialize views
initializeViews();
// Set up event listeners
setupEventListeners();
// Initialize selected hobbies list
selectedHobbies = new ArrayList<>();
}
private void initializeViews() {
checkboxReading = findViewById(R.id.checkboxReading);
checkboxMusic = findViewById(R.id.checkboxMusic);
checkboxSports = findViewById(R.id.checkboxSports);
checkboxTraveling = findViewById(R.id.checkboxTraveling);
buttonSubmit = findViewById(R.id.buttonSubmit);
textViewResult = findViewById(R.id.textViewResult);
}
private void setupEventListeners() {
// Method 1: Using OnCheckedChangeListener for individual checkboxes
checkboxReading.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckboxChange("Reading", isChecked);
}
});
checkboxMusic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckboxChange("Listening to Music", isChecked);
}
});
checkboxSports.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckboxChange("Playing Sports", isChecked);
}
});
checkboxTraveling.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckboxChange("Traveling", isChecked);
}
});
// Submit button click listener
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displaySelectedHobbies();
}
});
}
// Method to handle checkbox state changes
private void handleCheckboxChange(String hobby, boolean isChecked) {
if (isChecked) {
if (!selectedHobbies.contains(hobby)) {
selectedHobbies.add(hobby);
}
Toast.makeText(this, hobby + " selected", Toast.LENGTH_SHORT).show();
} else {
selectedHobbies.remove(hobby);
Toast.makeText(this, hobby + " deselected", Toast.LENGTH_SHORT).show();
}
// Update result text in real-time
updateResultText();
}
// Method to update result text view
private void updateResultText() {
if (selectedHobbies.isEmpty()) {
textViewResult.setText("No hobbies selected");
} else {
StringBuilder result = new StringBuilder("Selected Hobbies:\n");
for (int i = 0; i < selectedHobbies.size(); i++) {
result.append("โข ").append(selectedHobbies.get(i));
if (i < selectedHobbies.size() - 1) {
result.append("\n");
}
}
textViewResult.setText(result.toString());
}
}
// Method to display selected hobbies when submit is clicked
private void displaySelectedHobbies() {
if (selectedHobbies.isEmpty()) {
Toast.makeText(this, "Please select at least one hobby", Toast.LENGTH_LONG).show();
textViewResult.setText("No hobbies selected");
} else {
String message = "You have selected " + selectedHobbies.size() + " hobbies";
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
// Display detailed results
StringBuilder detailedResult = new StringBuilder("SUBMITTED HOBBIES:\n\n");
for (int i = 0; i < selectedHobbies.size(); i++) {
detailedResult.append((i + 1)).append(". ").append(selectedHobbies.get(i)).append("\n");
}
detailedResult.append("\nTotal: ").append(selectedHobbies.size()).append(" hobbies selected");
textViewResult.setText(detailedResult.toString());
}
}
// Alternative method: Using single listener for all checkboxes
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
String hobby = "";
switch (view.getId()) {
case R.id.checkboxReading:
hobby = "Reading";
break;
case R.id.checkboxMusic:
hobby = "Listening to Music";
break;
case R.id.checkboxSports:
hobby = "Playing Sports";
break;
case R.id.checkboxTraveling:
hobby = "Traveling";
break;
}
handleCheckboxChange(hobby, checked);
}
}
Key Features of this Implementation:โ
- Multiple Event Handling Methods: Shows both OnCheckedChangeListener and onClick methods
- Real-time Updates: Result updates as checkboxes are selected/deselected
- Toast Messages: Immediate feedback for user actions
- Validation: Checks if at least one hobby is selected
- Dynamic Text Building: Constructs result text dynamically
- ArrayList Management: Properly manages selected items list