Android Development Concepts: UI Components and Architecture

Posted by Anonymous and classified in Computers

Written on in English with a size of 11.09 KB

1. ListView vs RecyclerView Comparison

ListView

ListView is an older Android UI component used to display a scrollable list of items vertically. Each item is displayed one below another.

  • Uses Adapter to bind data.
  • Uses convertView for view reuse.
  • Only supports vertical scrolling.
  • Less efficient for large datasets.
  • No built-in animations.
  • ViewHolder pattern is optional.
  • Simple to use.
  • Suitable for small lists.

Example:

ListView listView = findViewById(R.id.listview); 
// ArrayAdapter<String> adapter = 
// new ArrayAdapter<>(this, 
// android.R.layout.simple_list_item_1, data); 
///listView.setAdapter(adapter);

RecyclerView

RecyclerView is an advanced and flexible version of ListView, introduced in Android.

  • Uses ViewHolder pattern by default.
  • Highly optimized for performance.
  • Supports multiple layouts.
  • Built-in animations.
  • Recommended by Google.
  • Better memory management.
  • Faster scrolling.
  • Supports Grid, Horizontal, Staggered layouts.
  • More flexible and reusable.

Example:

RecyclerView recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter(data));

2. Android Architecture Layers

Android architecture is a layered architecture describing how Android software components are organized. It consists of five main layers, where each upper layer uses the services of the lower layer.

  • Applications: Topmost layer. Includes system apps and user-installed apps (e.g., Phone, SMS, Browser, WhatsApp).
  • Application Framework: Provides APIs for app development (Activity Manager, Window Manager, Notification Manager).
  • Android Runtime (ART): Executes Android applications. Includes Core Libraries and converts bytecode into machine code.
  • Native Libraries: Written in C/C++. Provide core functionalities (Examples: SQLite, OpenGL, Media Framework, WebKit).
  • Linux Kernel: Base layer of Android. Handles hardware interaction, providing memory management, process management, device drivers, and security.

3. Creating AVDs and Using Spinner

AVD Creation Steps

AVD (Android Virtual Device) is an emulator configuration used to run and test Android applications on a computer.

  1. Open Android Studio.
  2. Click on Tools → Device Manager.
  3. Click Create Device.
  4. Select a hardware profile (e.g., Pixel).
  5. Choose Android version (System Image).
  6. Click Next → Finish.
  7. Click Run to start the emulator.

Using Spinner in Android

Steps to Use Spinner:

  1. Add Spinner in XML.
  2. Prepare data source.
  3. Use ArrayAdapter.
  4. Set adapter to Spinner.

Example:

Spinner spinner = findViewById(R.id.spinner); 
String[] items = {"Nepal", "India", "USA"}; 
ArrayAdapter<String> adapter = 
new ArrayAdapter<>(this, 
android.R.layout.simple_spinner_item, items);

adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

5. Mobile Application Development Approaches

Mobile application development approaches describe the different ways used to build mobile applications. The major approaches are Native, Web, Hybrid, and Cross-Platform development.

1. Native App Development

Native apps are developed specifically for a single platform using platform-specific languages and tools.

  • Examples: Android → Java / Kotlin; iOS → Swift / Objective-C.
  • Features: High performance, full access to device hardware (camera, GPS, sensors), best user experience, fast execution, better UI/UX, secure.

2. Web App Development

Web apps are accessed through a mobile browser and developed using web technologies (HTML, CSS, JavaScript).

  • Examples: Mobile websites, Progressive Web Apps (PWA).
  • Advantages: No installation required, single codebase, easy maintenance.

3. Hybrid App Development

Hybrid apps combine web technologies with a native container.

  • Technologies Used: HTML, CSS, JavaScript. Frameworks: Ionic, Cordova.
  • Advantages: Faster development, lower cost, access to basic device features.

4. Cross-Platform App Development

Cross-platform apps are developed using a single codebase and compiled into native applications.

  • Frameworks: Flutter, React Native, Xamarin.
  • Advantages: Single codebase for Android and iOS, near-native performance, faster development.

9. Generating Signed APK and Locating User Location

Signed APK Generation

A Signed APK is required to publish an Android app on the Google Play Store.

Locating User's Current Location (Java Code)

This code snippet demonstrates retrieving the last known location.

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// if (ActivityCompat.checkSelfPermission(this, 
// Manifest.permission.ACCESS_FINE_LOCATION) 
// != PackageManager.PERMISSION_GRANTED) { 
// return; 
// }

// Location location = 
// locationManager.getLastKnownLocation(
// LocationManager.GPS_PROVIDER);
// if (location != null) { 
// double latitude = location.getLatitude(); 
// double longitude = location.getLongitude(); 
// Toast.makeText(this, 
// "Lat: " + latitude + " Long: " + longitude, 
// Toast.LENGTH_LONG).show(); 
// }

7. iOS UI Controls and Array Sum

iOS UI Controls

UI controls are interactive elements used to get input from users and display information in iOS applications. In iOS, UI controls are mainly provided by the UIKit framework.

Common UI Controls:

  • UILabel: Used to display text (e.g., Display title or message).
  • UIButton: Used to perform an action when tapped (e.g., Submit, Login button).
  • UITextField: Used to input single-line text (e.g., Username, Password).
  • UITextView: Used to input or display multi-line text.
  • UIImageView: Used to display images.
  • UISwitch: Used for ON/OFF options (e.g., Enable notifications).
  • UISlider: Used to select a value from a range (e.g., Volume control).
  • UIProgressView: Shows progress of a task.
  • UISegmentedControl: Used to select one option from multiple segments.
  • UITableView: Used to display a list of data.

Swift Program to Find Sum of Array Elements

//import Foundation 
//let numbers = [10, 20, 30, 40, 50] 
//var sum = 0 
//for num in numbers { 
// sum += num 
//} 
//print("Sum of array elements = \(sum)")

8. Fragments: Uses and Differences from Activity

Uses of Fragments

A Fragment is a reusable portion of an Android user interface that runs inside an Activity. Fragments are mainly used to create flexible and modular UI designs.

  • To divide an activity into multiple UI sections.
  • To reuse UI components in multiple activities.
  • To support different screen sizes (mobile, tablet).
  • To create dynamic UI at runtime.
  • To improve UI management in complex applications.

Difference between Fragment and Activity

ActivityFragment
Activity is a complete screen.Fragment is a part of an activity.
Can exist independently.Cannot exist without an activity.
Managed by Android OS.Managed by Fragment Manager.
Has its own lifecycle.Has lifecycle dependent on activity.
Declared in AndroidManifest.xml.Not declared in Manifest.
Heavy component.Lightweight component.

6. Toast and Factorial Calculation with AlertDialog

Toast Definition

A Toast is a small message that pops up on the screen for a short time to provide feedback to the user. It does not require user interaction and disappears automatically.

Syntax:

Toast.makeText(context, "Message", Toast.LENGTH_SHORT).show();

AlertDialog for Factorial Calculation

An AlertDialog is used to take numeric input from the user. The entered number is used to calculate factorial, and the result is displayed using Toast.

Steps to Develop the Application:

  1. Create a new Android project.
  2. Add a Button in the layout.
  3. Show AlertDialog with EditText on button click.
  4. Calculate factorial.
  5. Display result using Toast.

4. Developing an Android Application to Display AlertDialog

AlertDialog Purpose

An AlertDialog is used to show important messages, warnings, or confirmations to the user with buttons like OK, Cancel, Yes, or No.

Steps to Develop Alert Dialog Application:

  1. Create a new Android project in Android Studio.
  2. Design the UI (Button to show dialog).
  3. Create AlertDialog using AlertDialog.Builder.
  4. Display dialog using show() method.
Example: XML Layout (activity_main.xml)
<Button 
 android:id="@+id/btnAlert" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Show Alert" 
 android:layout_centerInParent="true"/>
Java Code (MainActivity.java):
Button btn = findViewById(R.id.btnAlert);
btn.setOnClickListener(v -> {
    AlertDialog.Builder builder =
        new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Alert")
        .setMessage("Do you want to continue?")
        .setPositiveButton("Yes", null)
        .setNegativeButton("No", null)
        .setCancelable(false);
    builder.show();
});

Related entries: