Android
Android is an open source operating system developed by Google, based on a modified version of linux kernel.
The Android Architecture has 5 main components:
Android Architecture | Description |
---|---|
Linux Kernel | Memory Management, Security, Process Management etc. |
Platform Libraries | Support for Databases, Graphics, Media etc. (e.g., SQLite, WebKit) |
Android Runtime | Runtime Environment. A Java Virtual Machine designed for Android |
Application Framework | High-level services such as Activity Management, Window Management etc. |
Applications | User-Installed apps that run on top of the application framework |
Android Components | Purpose | Example |
---|---|---|
Activities | Represents a single screen with a UI. Handles user interactions, displays information and manages the application’s visual elements. | A Login Screen, Settings Screen |
Services | Runs in the background to perform long-running operations without a UI. | Music Player service, Downloading files, Syncing data |
Broadcast Recievers | Responds to system-wide broadcast messages. Listens for events. | SMS alert, Battery level listeners |
Content Providers | Manages and shares structured data with other applications. Provides a consistent interface for accessing data stored in various formats (e.g. SQLite) | Contact app’s content provider allows other apps to access contacts |
Android Studio
Android Studio is the official IDE for Android OS, based on JetBrains IntelliJ IDEA software. It provides a comprehensive suite of tools to help developers create, test and debug Android applications.
Android Studio uses Gradle to manage the build process, packaging APKs etc.
Getting Started
Android Studio can be downloaded from the official page.
To create a new project, run File -> New -> New Project. Now select a project template (e.g., Empty Views Activity), configure the project by providing a name, save location etc. and click Finish.
Create a new virtual device to run the app on by Device Manager -> + -> Create Virtual Device
Click Run ‘app’ (Shift + F10) to run on the emulator or smartphone connected via WiFi or USB.
If Gradle Build fails, make sure
compileSdk
andtargetSdk
is set to34
on Gradle Scripts -> build.gradle.kts
Project Structure
app
├── manifests
│ └── AndroidManifest.xml
├── java
│ └── com.example.myapplication
│ └── MainActivity.java
└── res
├── drawable
├── layout
│ └── activity_main.xml
├── mipmap
├── values
└── xml
Gradle Scripts
app
is the main module of the application.
Metadata, permissions, etc. is contained in the AdroidManifest.xml
in the manifests
folder.
java
folder contains the Java / Kotlin source code.
All the non-code resources are located in the res
folder, such as layouts (layout
), images (drawable
), icons (mipmap
), strings (values
), XML etc.
Gradle Scripts
define how the project is built and managed.
The xml
could be considered as the frontend and the java
file as the backend of our application.
Widgets and Layouts
Widgets are the basic building blocks of an Android application’s UI. They are interactive components that users can interact with. Common Widgets include:
- Button
- TextView: Displays text.
- EditText: Text input field.
- ImageView: Displays an image.
- CheckBox
- RadioButton
- ProgressBar
Layouts are containers that define the structure for a UI in an app. They hold widgets and other layouts, arranging them on the screen according to specific rules. Common Layouts include:
- LinearLayout: Arranges its children in a single row or column.
- RelativeLayout: Arranges its children in relation to each other or to the parent container.
- ConstraintLayout: A flexible layout that allows you to position and size widgets.
- FrameLayout: A simple layout that can hold one child view, making it useful for displaying a single item.
- GridLayout: Arranges its children in a grid.
Layouts are a type of ViewGroup. ViewGroup is a subclass of View that can contain other Views. It acts as a container for other views. View is the simplest UI component in Android. Widgets are a specific type of View.
Class Hierarchy in Java:
The class hierarchy in Java for Android UI components is as follows:
- Object
- View
- ViewGroup
- Widgets
- View
To build the app as an APK, go to Build -> Build App Bundles / APKs -> Build APKs
Features
GridLayout
GridLayout is a layout manager that allows you to place child elements in a grid of rows and columns.
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="3">
<!-- Child elements -->
</GridLayout>
Intent
An Intent provides binding between two components, such as two activities.
To create a new Activity inside our app, do File -> New -> Activity
Intent is used to start another activity, start a service, deliver a broadcast, etc.
1. Explicit Intent
Explicit intents specify the component in the same app to start, by name (of the class).
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", "value"); // Passing data to the next component
startActivity(intent);
// Accessing the passed data
Intent intent = getIntent();
String value = intent.getStringExtra("key");
2. Implicit Intent
Implicit intents do not name a specific component but declare a general action to perform, which allows a component from another app to handle it.
// Launch a browser to display a specified URL
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.example.com"));
startActivity(intent);
// Dial a phone number
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:+123456789"));
startActivity(intent);
// Send an email
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:example@example.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Email body");
startActivity(intent);
// Share text content with other apps
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "This is the text to share.");
startActivity(Intent.createChooser(intent, "Share via"));
Toast
A Toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive.
Toast.makeText(getApplicationContext(), "Hello, World!", Toast.LENGTH_SHORT).show();
Spinner
A Spinner is a widget that allows the user to select an item from a dropdown list.
Spinner spinner = findViewById(R.id.spinner);
String[] planets = new String[] {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, planets);
spinner.setAdapter(adapter);
Activity Lifecycle
An activity is a fundamental component that manages the user interface and interactions of an application.
An Activity in Android goes through different states during its lifetime. These states are managed by the system and the developer can override the methods to handle these states.
onCreate()
: Activity is created.onStart()
: Activity becomes visible to the user.onResume()
: Activity starts interacting with the user.onPause()
: Activity goes into the background.onStop()
: Activity is no longer visible.onRestart()
: Activity is restarted from the stopped state.onDestroy()
: Called before the activity is destroyed.
Logs
Logs are used to print out messages that help the developer understand the flow of the code, to the log (LogCat).
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity"; // Common practice
// ...
Log.d(TAG, "Debug message");
Log.v(TAG, "Verbose message");
Log.i(TAG, "Info message");
Log.w(TAG, "Warning message");
Log.e(TAG, "Error message");
// ...
}
Scroll View
A ScrollView is a special type of layout that allows its only child to be scrolled vertically.
To make multiple elements scrollabe, we need to put a layout inside ScrollView
, for example LinearLayout
.
List View
A ListView displays a list of items in a vertical, scrollable list.
ArrayAdapter
ArrayAdapter
provides a binding from an array of data to views that are displayed within a ListView
, Spinner
etc.
String[] countries = {"USA", "Canada", "Mexico", "UK", "Germany"};
// Create the ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, countries);
// Set the adapter to the listview
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
Shared Preferences
Shared Preferences is a local storage area used to store and retrieve primitive data.
Data is stored in XML files within the app’s private storage.
It is a light weight mechanism to store a known set of values like storing UI states (favourites, stars), user preferences (game level), application settings (themes) etc.
We can create or modify shared preferences via getSharedPreferences(key, mode)
.
Modes include public (MODE_PUBLIC
), private(MODE_PRIVATE
) and append(MODE_APPEND
).
SharedPreferences.Editor
is used to write or edit data in the SP file.SharedPreferences.OnSharePreferenceChangeListener()
listens to changes in the shared preferences file.
// Get the SharedPreferences instance
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
// Get the SharedPreferences.Editor instance
SharedPreferences.Editor editor = sharedPreferences.edit();
// Store data
editor.putString("username", "JohnDoe");
editor.putInt("userAge", 25);
editor.putBoolean("isLoggedIn", true);
// Commit the changes
editor.apply(); // or editor.commit();
// Get the SharedPreferences instance
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
// Retrieve data
String username = sharedPreferences.getString("username", "defaultName");
int userAge = sharedPreferences.getInt("userAge", 0);
boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);
// Use the retrieved data
References: