Share
Explore

Introduction to Android Development

Session 1 : March 13

Reference: Big Nerd Ranch Guide: Chapters 1, 4


Using Android Studio:

You can press the SHIFT KEY twice to bring up a panel to search for files by name.

What is Android?

Android is an open source and Linux-based Operating System for mobile devices such as smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by Google, and other companies.
Android offers a unified approach to application development for mobile devices which means developers need to develop only for Android, and their applications should be able to run on different devices powered by Android.
The first beta version of the Android Software Development Kit (SDK) was released by Google in 2007, whereas the first commercial version, Android 1.0, was released in September 2008.
On June 27, 2012, at the Google I/O conference, Google announced the next Android version, 4.1 Jelly Bean. Jelly Bean is an incremental update, with the primary aim of improving the user interface, both in terms of functionality and performance.
The source code for Android is available under free and open source software licenses. Google publishes most of the code under the Apache License version 2.0 and the rest, Linux kernel changes, under the GNU General Public License version 2.

Introduction to Android

- Android application architecture and devices - Fundamentals of the development environment and tools (Android Studio)
Let’s Get Started:

Activities

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and one for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.
image.png
image.png

The AndroidManifest.xml file is the center of and controller of your Application.

The AndroidManifest.xml file plays an essential role in the development of Android applications. The AndroidManifest.xml file is a required file that must be included in any Android app, and it contains information about the app, including the app's package name, version, permissions, and components such as activities, services, broadcast receivers, and content providers.

Activities are a key component of Android applications, and they are defined in the AndroidManifest.xml file.

image.png
Each activity must have a unique name and a corresponding layout file that defines the UI of the activity.
The layout file is usually named activity_main.xml and is stored in the res/layout folder of the app's directory.
The layout file defines the actual UI of the app and contains all the views or widgets, such as buttons, text views, edit texts, and more, which are organized under view groups.
The layout file is responsible for defining the positioning, size, and style of each view or widget in the UI.
It's important to note that the layout file and the AndroidManifest.xml file are separate files, but they work together to define the UI and behavior of the app.
The layout file defines the UI of the activity, and the AndroidManifest.xml file defines the activity itself, including its name, intent filters, and permissions.
In summary, the AndroidManifest.xml file plays a crucial role in Android app development, as it contains information about the app, including its components such as activities.
The activity itself is defined in the AndroidManifest.xml file, and each activity has a corresponding layout file that defines the UI of the activity. Both files work together to define the UI and behavior of the app.
References:
[1] GeeksforGeeks. (2022, February 9). A Complete Guide to Learn XML for Android App Development.
[2] Android Authority. (2019, February 12). Introduction to XML.
[3] JavaTpoint. (n.d.). AndroidManifest.xml file in android.
[4] AbhiAndroid. (n.d.). XML in Android - An Introduction.
[5] GeeksforGeeks. (2022, February 9). Introduction to Activities in Android.

Android Studio Auto Import Feature

Please ensure your Android Studio has Auto Import turned on
On Windows: File > Settings > Editor > General > Auto Import.
image.png
On Mac: Android Studio > Preferences
image.png


The Architecture of Android Applications

image.png
image.png
image.png
image.png

Android Application Development: Building UIs with Activities

Android application development involves creating apps for mobile devices using the Android operating system. One of the essential components in building an Android application is the use of activities.
The basic elements of building an Android app and show their interconnection
When building an Android app, there are several basic elements that are interconnected and necessary to ensure the app functions properly. The following elements are crucial in the development process:
Activities: These are the main components of an app's user interface (UI), and represent a single screen with which the user interacts. Each activity is a separate entity that can be called upon by other activities or the operating system, and they can communicate with each other using intents ​[1] Android Developers. (n.d.). App components. Android Developers.
[2] Vogel, L. (2021, January 11). The basics of Android app development. Ray Wenderlich.
Services: These are components that perform background tasks without a UI, such as playing music or downloading files. Services run in the background and can communicate with activities using intents [1][2].
Broadcast Receivers: These are components that respond to system-wide events, such as a low battery or incoming call. They can be registered to listen for specific events and then execute a task when the event occurs [1][2].
Content Providers: These are components that manage shared data, such as contacts or files. Content providers allow different apps to access the same data, and they can enforce security and permissions on that data [1][2].
These elements are interconnected through the use of intents, which are messages that allow different components of an app to communicate with each other. For example, an activity can send an intent to a service to start playing music, and the service can send an intent back to the activity when the music is finished.
Overall, the basic elements of building an Android app include activities, services, broadcast receivers, and content providers. These elements are interconnected using intents, which allow different components to communicate with each other and work together to create a functional app.
References:
[1] Android Developers. (n.d.). App components. Android Developers.
[2] Vogel, L. (2021, January 11). The basics of Android app development. Ray Wenderlich.

Activities

An activity represents a single screen with a user interface. In other words, it is one of the building blocks of an Android application. An application can have multiple activities, and each activity can have its user interface.

Android User Interface Basics: Learning Outcomes

- Introduction to Views (TextView, EditText, Button, ImageView) - Handling event listeners for various UI Components

Lab Activity: Making a simple Android App with KOTLIN

Student Lab Workbook: Making a Simple Android App with Kotlin

Learning outcomes:
How to create a simple Android app using the Kotlin programming language.
The app will display a list of items and allow the user to add new items to the list.

Prerequisites

Android Studio installed on your computer
Basic knowledge of Kotlin programming language
Basic knowledge of Android app development

Step 1: Create a New Project

Open Android Studio and click on "Start a new Android Studio project" from the welcome screen.
Choose "Empty Activity" as the project template.
Enter a name for your project and choose a package name.
Choose Kotlin as the programming language.
Click "Finish" to create the new project.

Step 2: Design the App UI

Open the activity_main.xml file.
Drag and drop a RecyclerView from the "Layouts" section of the Palette onto the design editor. (DOUBLE SHIFT TO FIND layout.xml file): ​
image.png
Set the layout_width and layout_height attributes of the RecyclerView to match_parent.
Set the id attribute of the RecyclerView to recyclerView.
Enter a name for the layout file (e.g. item_layout) and choose "LinearLayout" as the root element.
Add a TextView and an EditText to the LinearLayout.
Set the layout_width and layout_height attributes of the TextView and EditText to wrap_content.
Set the hint attribute of the EditText to "Enter item name".
Save the layout file.

In Android Studio's activity_main.xml, you may see two panels showing a white background and a dark background. These two panels represent two different views of your layout file.
The white background panel is the Design view, which provides a graphical representation of your layout. This view allows you to drag and drop UI components onto your layout and visualize how your app will look like on different screen sizes and orientations. The Design view is intended to help you design your layout visually without worrying too much about the XML code.
The dark background panel is the Text view, which displays the XML code of your layout file. This view allows you to edit the XML code directly and provides a more precise control over your layout. You can use this view to fine-tune your layout or add advanced attributes that are not available in the Design view.
You can switch between the Design view and Text view by clicking on the respective tabs at the bottom of the editor window. You can also split the editor window vertically to display both views side by side. This can be useful when you want to see the graphical representation of your code changes in real-time.
It is important to note that the two panels are synchronized, meaning that any changes you make in one view will be reflected in the other view. So, whether you prefer the Design view or Text view, you can use whichever view suits your needs best.

Step 3: Create the Data Model

Create a new Kotlin file by right-clicking on the package name and choosing "New -> Kotlin File/Class".
Enter a name for the file (e.g. Item) and choose "Class" as the kind.
Add two properties to the class: name (String) and id (Int).
Add a companion object with a list of sample items.
Save the file.
class Item(val name: String, val id: Int) {
companion object {
val sampleItems = listOf(
Item("Item 1", 1),
Item("Item 2", 2),
Item("Item 3", 3),
Item("Item 4", 4),
Item("Item 5", 5)
)
}
}

Step 4: Create the Adapter

Create a new Kotlin file by right-clicking on the package name and choosing "New -> Kotlin File/Class".
Enter a name for the file (e.g. ItemAdapter) and choose "Class" as the kind.
Extend the RecyclerView.Adapter class.
Create a ViewHolder class inside the ItemAdapter class.
Inflate the item_layout inside the ViewHolder constructor.
Bind the name property of the Item to the TextView.
Save the file.
class ItemAdapter(private val items: List<Item>) : RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)

fun bind(item: Item) {
nameTextView.text = item.name
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return ViewHolder(view)
}
a
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(items[position])
}

override fun getItemCount(): Int {
return items.size
}
}

Step 5: Create the Main Activity

Open the MainActivity.kt file.
Remove the onCreateOptionsMenu and onOptionsItemSelected methods.
Replace the contents of the onCreate method with the following code:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = ItemAdapter(Item.sampleItems)
}

Save the file.

Step 6: Add New Items

Open the activity_main.xml file.
Add a Button below the RecyclerView.
Set the layout_width and layout_height attributes of the Button to wrap_content.
Set the id attribute of the Button to addButton.
Set the text attribute of the Button to "Add Item".
Save the file.


Step 7: Handle Button Clicks

Open the MainActivity.kt file.
Add the following code at the end of the onCreate method:
val addButton: Button = findViewById(R.id.addButton)
addButton.setOnClickListener {
val editText: EditText = findViewById(R.id.editText)
val itemName = editText.text.toString()
val newItem = Item(itemName, Item.sampleItems.size + 1)
Item.sampleItems += newItem
recyclerView.adapter?.notifyItemInserted(Item.sampleItems.size - 1)
editText.text.clear()
}

Save the file.

Step 8: Run the App

Click on the "Run" button in Android Studio.
Choose "app" as the target device and click "OK".
Wait for the app to build and deploy.
Test the app by adding new items to the list.
Congratulations, you have successfully created a simple Android app using Kotlin programming language. The app will display a list of items and allow the user to add new items to the list.


Session 2: March 14 : Android User Interface Basics

Android Developer Documentation: User Interface
Intro to Views (TextView, EditText, Button, ImageView)
Handling event listeners for various UI Components

Android Main Building Blocks

- Introduction to Activities - Activity Life Cycle
Examples: Using a TextView

Example: Using an EditText

Example: Using a ImageView

Example: Using a Button

Example: Using a Radio Group

Example: Using a Button

Example: Retrieve two numbers from text boxes, then output the sum to the screen



To fix the Android Studio error "Device manager The emulator process for AVD Pixel_2_API_3- has been terminated," follow these instructions:

Update the Android Emulator:
a. Open Android Studio and go to Tools > SDK Manager.
b. In the SDK Manager, navigate to the SDK Tools tab.
c. Untick the Android Emulator checkbox and click Apply to uninstall the current version.
d. After the uninstallation is complete, tick the Android Emulator checkbox and click Apply to reinstall the emulator [
].
Set a custom environment variable for the emulator:
a. Create a new directory (for example, C:\Androidhome).
b. Set the ANDROID_SDK_HOME environment variable to the newly created directory [
].
Verify emulator connectivity:
a. Launch the emulator from the Android Device Manager (select your virtual device and click Start).
b. Open a command prompt and navigate to the folder where adb is installed. The default location for adb is C:\Program Files (x86)\Android\android-sdk if the Android SDK is installed at its default location.
c. Follow further instructions as needed to verify emulator connectivity [
].
These steps should help resolve the "emulator process terminated" error in Android Studio.


March 14 Session 2:

Building the Simple App
View, Fields, ONClick

Student Lab Workbook: Basic Android App with Kotlin

Introduction

Welcome to the lab workbook for creating a basic android app using Android Studio Electric Eel 2022.1.1 and Kotlin.
In this lab, you will learn how to create a simple app that displays a message when a button is pressed, without using fragments.

Prerequisites

Before beginning this lab, you should have some basic knowledge of Kotlin programming and the Android Studio environment.

Lab Steps

Step 1: Create a new project

Open Android Studio and select "Start a new Android Studio project." Choose "Empty Activity" as the project template and give your project a name.

Step 2: Design the UI

Open the layout file
add a button and
a text view to your layout
Set the text of the button to "Press Me" and the text of the text view to "Hello World!".

Step 3: Add functionality to the button

Open the Main Activity file and create a function to handle button clicks. When the button is clicked, the text in the text view should change to "Button Pressed!".
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val button = findViewById<Button>(R.id.button)
val textView = findViewById<TextView>(R.id.textView)

button.setOnClickListener {
textView.text = "Button Pressed!"
}
}
}

Step 4: Test the app

Run the app on an emulator or a physical device to test its functionality.

Conclusion

Congratulations! You have successfully created a basic android app using Android Studio Electric Eel 2022.1.1 and Kotlin, without using fragments. This lab has provided you with a foundation to build upon for more complex app development in the future.


Student Lab Workbook: Basic Android App with Kotlin

Introduction

Welcome to the lab workbook for creating a basic android app using Android Studio Electric Eel 2022.1.1 and Kotlin. In this lab, you will learn how to create a simple app that displays a message when a button is pressed, without using fragments.

Prerequisites

Before beginning this lab, you should have some basic knowledge of Kotlin programming and the Android Studio environment.

Lab Steps

Step 1: Create a new project

Open Android Studio and select "Start a new Android Studio project." Choose "Empty Activity" as the project template and give your project a name.

Step 2: Design the UI

Open the layout file and add a button and a text view to your layout. Set the text of the button to "Press Me" and the text of the text view to "Hello World!".

Step 3: Add functionality to the button

Open the Main Activity file and create a function to handle button clicks.
When the button is clicked, the text in the text view should change to "Button Pressed!".
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val button = findViewById<Button>(R.id.button)
val textView = findViewById<TextView>(R.id.textView)

button.setOnClickListener {
textView.text = "Button Pressed!"
}
}
}

Step 4: Test the app

Run the app on an emulator or a physical device to test its functionality.

Conclusion

Congratulations! You have successfully created a basic android app using Android Studio Electric Eel 2022.1.1 and Kotlin, without using fragments. This lab has provided you with a foundation to build upon for more complex app development in the future.

write a lab notebook teaching students how to use every aspect of the Attribute Editor for Android Studio Android Studio Electric Eel | 2022.1.1

Lab Notebook: Using the Attribute Editor in Android Studio Electric Eel 2022.1.1

Introduction

The Attribute Editor in Android Studio is a powerful tool that allows you to customize the properties of your layout elements. In this lab, you will learn how to use every aspect of the Attribute Editor in Android Studio Electric Eel 2022.1.1.

Prerequisites

Before beginning this lab, you should have some basic knowledge of Android Studio and layout design.

Lab Steps

Step 1: Open the Attribute Editor

Open Android Studio and open your layout file in the design view. Select any layout element, such as a button or a text view, and open the Attribute Editor by clicking on the "Attributes" tab at the bottom of the design view.

Step 2: Edit View Properties

In the Attribute Editor, you can edit the properties of the selected view. Some of the commonly used properties include:
Text: The text displayed in the view.
Background: The background color or image of the view.
Layout Width and Height: The size of the view.
Padding: The space between the view's content and its border.
Visibility: Whether the view is visible or not.

Step 3: Edit Text Appearance

When editing text views, you can also edit the appearance of the text. In the Attribute Editor, you can change the font family, font size, text color, and other properties.

Step 4: Edit Layout Properties

You can also edit the properties of the layout itself in the Attribute Editor. Some of the commonly used properties include:
Layout Gravity: The alignment of the layout within its parent.
Orientation: The orientation of the layout.
Padding: The space between the layout's content and its border.

Step 5: Edit Custom Attributes

If you have created custom attributes for your views, you can edit them in the Attribute Editor as well.
Simply scroll to the bottom of the Attribute Editor and expand the "Custom Attributes" section.

Step 6: Use the Visual Editor

In addition to editing attributes in the Attribute Editor, you can also use the visual editor to drag and drop elements onto your layout.
You can then edit the properties of these elements in the Attribute Editor.

Step 7: Save Changes

Once you have made the desired changes to your layout's attributes, be sure to save your changes by clicking "Save" in the top left corner of the Android Studio window.

Conclusion

Congratulations! You have learned how to use every aspect of the Attribute Editor in Android Studio Electric Eel 2022.1.1. This lab has provided you with a foundation to customize your layout elements to your exact specifications. Keep practicing and experimenting with the Attribute Editor to become a layout design master.


Student Lab Workbook: Android Project with Kotlin

Introduction

In this lab, we will be creating an Android project using Kotlin in Android Studio Electric Eel 2022.1.1. The project will consist of a button and two image views, all of which will communicate with each other in response to onclick events.

Prerequisites

Before starting this lab, you will need to have the following:
Android Studio Electric Eel 2022.1.1 installed on your computer
Basic knowledge of Kotlin programming language
Basic understanding of Android app development and Android Studio IDE

Steps

1. Create New Project

Open Android Studio and create a new project.
Give your project a name and select Kotlin as the programming language.

2. Design the User Interface

Open the activity_main.xml file and design the user interface.
Add a button and two image views to the layout.
Here is an example of how the layout should look:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:layout_centerInParent="true"/>

<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/image1"
android:layout_above="@+id/imageView2"
android:layout_marginBottom="50dp"
android:layout_centerHorizontal="true"/>

<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/image2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

3. Define Variables and Find Views

Open the MainActivity.kt file and define variables for the button and image views. You will also need to find the views in the layout using their respective IDs. Here is an example of how to do this:
class MainActivity : AppCompatActivity() {

private lateinit var button: Button
private lateinit var imageView1: ImageView
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.