Mobile ProgrammingUnit 112 min read
Mobile Programming & Android Basics: Setup, SDK, IDE, and First App
Unit 1 of Mobile Programming: Introduces the mobile app ecosystem, Android’s role in global markets, the Android SDK and IDE setup, project structure, and writing your first app using Android Studio with XML layouts and Java/Kotlin.
TAKEAWAYS:
- Android powers 70% of global smartphones (Google Play Store has 3.5M+ apps), making it the dominant mobile OS for developers.
- The Android SDK provides tools (AVD, emulator, ADB) and libraries (AndroidX) to build apps, while Android Studio is the official IDE with drag-and-drop UI design.
- Every Android app has a hierarchical directory structure (e.g.,
res/,src/,AndroidManifest.xml) that organizes resources, code, and configurations. - XML layouts define UI components (e.g.,
LinearLayout,Button) declaratively, while Java/Kotlin handles logic and events. - String and string arrays in Android enable dynamic UI text and localized content (e.g., app names in multiple languages).
- Your first app follows a boilerplate structure:
MainActivity(extendsAppCompatActivity),setContentView(R.layout.activity_main), and event handlers (e.g.,onClick).
1. The Mobile App Ecosystem
Mobile apps are software applications designed for smartphones and tablets, enabling functionalities like communication, productivity, entertainment, and commerce. Android, developed by Google, dominates the market with over 2.5 billion active users (as of 2023), followed by iOS (Apple).
Why Android?
| Feature | Android | iOS (Apple) |
|---|---|---|
| Open Source | Yes (Linux kernel-based) | No (proprietary) |
| Market Share | ~70% globally | ~30% globally |
| Customization | High (OEMs modify UI/OS) | Low (uniform across devices) |
| Development Cost | Low (free tools, open SDK) | High (Mac required, paid tools) |
| Ecosystem | Google Play Store (3.5M+ apps) | App Store (1.8M+ apps) |
(Note: The site will replace this with a bar chart showing Android’s ~70% lead.)
2. Android SDK and IDE Setup
To develop Android apps, you need:
- Android SDK (Software Development Kit): Tools, libraries, and documentation to build apps.
- Android Studio: Official IDE (Integrated Development Environment) with:
- Code editor (Kotlin/Java)
- Layout designer (drag-and-drop UI)
- Emulator (AVD for testing)
- Debugger (ADB for device debugging)
Installation Steps (Android Studio)
graph TD
A["Download Android Studio"] --> B["Install JDK (Java 11+)"]
B --> C["Open Android Studio"]
C --> D["Create New Project"]
D --> E["Select Empty Activity Template"]
E --> F["Configure SDK Path"]
F --> G["Build & Run"]Key Tools in Android SDK:
- AVD (Android Virtual Device): Emulates different Android versions/device specs.
- ADB (Android Debug Bridge): Communicates with emulators/devices for debugging.
- AndroidX: Modernized libraries (replaces older
Support Library).
3. Android Project Structure
Every Android app has a hierarchical directory structure in app/:
app/
├── src/
│ ├── main/
│ │ ├── java/ # Java/Kotlin code
│ │ ├── res/ # Resources (images, layouts, strings)
│ │ │ ├── layout/ # XML layouts (e.g., `activity_main.xml`)
│ │ │ ├── values/ # Strings, colors, styles
│ │ │ └── drawable/ # Images (icons, backgrounds)
│ │ └── AndroidManifest.xml # App metadata (permissions, components)
│ └── AndroidManifest.xml # (Root-level, if not in `main/`)
Visual: Directory Structure ├── app/ │ ├── src/ │ │ ├── main/ │ │ │ ├── java/com/example/myapp/ │ │ │ │ └── MainActivity.java │ │ │ ├── res/ │ │ │ │ ├── layout/ │ │ │ │ │ └── activity_main.xml │ │ │ │ └── values/ │ │ │ │ └── strings.xml │ │ │ └── AndroidManifest.xml │ └── build.gradle └── gradle.properties Caption: Typical Android project structure in Android Studio.
4. Writing Your First App
Example: Simple Interest Calculator
Requirements:
- Input fields for principal (P), rate (R), and time (T).
- Button to calculate simple interest (SI = P × R × T / 100).
- Display result in a
TextView.
Step 1: Design the UI (XML Layout)
<!-- res/layout/activity_main.xml -->
<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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Interest Calculator"
android:textSize="18sp"
android:textStyle="bold"/>
<EditText
android:id="@+id/etPrincipal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Principal (P)"/>
<EditText
android:id="@+id/etRate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Rate (R)"/>
<EditText
android:id="@+id/etTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Time (T)"/>
<Button
android:id="@+id/btnCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"/>
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result:"/>
</LinearLayout>
Step 2: Implement Logic (Kotlin)
// MainActivity.kt
package com.example.simpleinterest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnCalculate = findViewById<Button>(R.id.btnCalculate)
val etPrincipal = findViewById<EditText>(R.id.etPrincipal)
val etRate = findViewById<EditText>(R.id.etRate)
val etTime = findViewById<EditText>(R.id.etTime)
val tvResult = findViewById<TextView>(R.id.tvResult)
btnCalculate.setOnClickListener {
val p = etPrincipal.text.toString().toDouble()
val r = etRate.text.toString().toDouble()
val t = etTime.text.toString().toDouble()
val si = p * r * t / 100
tvResult.text = "Simple Interest: ₹$si"
}
}
}
Step 3: Run the App
- Click Run (▶) in Android Studio.
- Select an AVD or connected device.
- Enter values (e.g., P=1000, R=5, T=2) and click Calculate.
- Output:
Simple Interest: ₹100.
Trace Table:
| Step | Action | UI State |
|---|---|---|
| 1 | Open app | Empty input fields, "Result:". |
| 2 | Enter P=1000, R=5, T=2 | Fields populated. |
| 3 | Click Calculate | tvResult updates to "Simple Interest: ₹100". |
5. String and String Arrays in Android
Strings in Android are stored in res/values/strings.xml for localization (e.g., app names in Nepali, Hindi, English).
Example: Localized Greeting
<!-- res/values/strings.xml -->
<resources>
<string name="app_name">MyApp</string>
<string name="greeting">Hello, %s!</string>
</resources>
// MainActivity.kt
val greeting = getString(R.string.greeting, "User")
Toast.makeText(this, greeting, Toast.LENGTH_SHORT).show()
// Output: "Hello, User!"
String Arrays:
<!-- res/values/arrays.xml -->
<resources>
<string-array name="programming_languages">
<item>Java</item>
<item>Kotlin</item>
<item>Python</item>
<item>C++</item>
</string-array>
</resources>
val langs = resources.getStringArray(R.array.programming_languages)
for (lang in langs) {
Log.d("Lang", lang) // Prints Java, Kotlin, Python, C++
}
6. Android Architecture Overview
Android apps follow the client-server model, where:
- Client: User’s device (Android app).
- Server: Backend (e.g., Firebase, custom APIs).
Key Components:
- Activities: Single screen with UI (e.g.,
MainActivity). - Services: Background tasks (e.g., music playback).
- Broadcast Receivers: Handle system events (e.g., SMS).
- Content Providers: Share data between apps (e.g., Contacts).
Visual: Android Architecture
graph TD
A["User"] --> B["Android App (Client)"]
B --> C["Activities"]
B --> D["Services"]
B --> E["Broadcast Receivers"]
B --> F["Content Providers"]
B --> G["Android OS"]
G --> H["Linux Kernel"]
G --> I["Java/Kotlin Runtime"]
G --> J["Android Framework"]Exam Tip: Always mention Activities and Services when explaining Android architecture.
In the Real World
eSewa (Nepal)
- Idea Used: Android Architecture (Activities + Services)
- How: eSewa’s app uses multiple
Activitiesfor login, payment, and transaction history. A Service runs in the background to sync user data with its servers. - Worked Example: When you open eSewa, the
MainActivityloads, and aServicechecks for pending transactions in real-time.
Daraz (Nepal)
- Idea Used: String Arrays + Layouts
- How: Daraz displays product categories (e.g., Electronics, Groceries) using string arrays in
ListVieworRecyclerView. The UI is built with XML layouts for dynamic item rendering. - Worked Example: The "Shop by Category" screen uses a
StringArrayto populate dropdown menus.
Pathao (Nepal)
- Idea Used: Activities + Intents
- How: Pathao’s app uses
Activitiesfor rider selection, payment, and ride tracking. Intents pass data between activities (e.g., from "Select Rider" to "Confirm Ride"). - Worked Example: After selecting a rider, an
Intenttriggers thePaymentActivityto process the fare.
Exam Tips
For Practical Questions (e.g., "Develop an app to calculate perimeter of a rectangle"):
- Always use a
LinearLayoutfor simple UIs. - Follow this structure:
flowchart TD A["XML Layout"] --> B["Define EditText for length/width"] B --> C["Define Button"] C --> D["Define TextView for result"] D --> E["Kotlin/Java Logic"] E --> F["Calculate perimeter = 2*(length + width)"] - Marks Breakdown:
- 2 marks: Correct XML layout.
- 3 marks: Proper event handling (e.g.,
onClick). - 2 marks: Correct calculation logic.
- 1 mark: Error handling (e.g., empty input).
- Always use a
For Theoretical Questions (e.g., "Explain Android architecture"):
- Draw the architecture diagram (as above) and label:
- Activities (UI screens).
- Services (background tasks).
- Broadcast Receivers (event handling).
- Content Providers (data sharing).
- Mention Android OS layers: Linux Kernel → Java/Kotlin Runtime → Android Framework.
- Draw the architecture diagram (as above) and label:
For String/Array Questions:
- Always show
strings.xmlandarrays.xmlsnippets. - Explain how
getString()andgetStringArray()work. - Example:
val name = getString(R.string.app_name) // "MyApp" val langs = getStringArray(R.array.languages) // ["Java", "Kotlin", ...]
- Always show
For Directory Structure Questions:
- Draw the tree structure (as shown earlier) and label:
src/main/java/→ Code.res/layout/→ XML files.AndroidManifest.xml→ App metadata.
- Mention
res/values/for strings, colors, and styles.
- Draw the tree structure (as shown earlier) and label:
Common Mistakes to Avoid:
- Forgetting
setContentView(R.layout.activity_main)inonCreate(). - Using
TextViewinstead ofEditTextfor user input. - Not handling empty inputs (e.g.,
if (etPrincipal.text.isEmpty())).
- Forgetting
Final Note: Always test your app on multiple screen sizes (emulator or physical device) and check for null pointer exceptions in logs. Use Logcat (Tools > Android > Logcat) to debug.
Based on the TU BCA syllabus for Mobile Programming (CACS351), unit 1.
Discussion
Loading…