Task 2: Create Your First Android App Project
Beginning Your Journey in Android Development
Dive into the creation of your first Android project. This step is where you lay the groundwork for a basic app that will display "Hello World" on either a virtual or physical Android device.
You'll gain hands-on experience in:
- Creating a new project in Android Studio.
- Setting up an emulated Android device.
- Running your app in an emulator.
- Deploying your app on a physical device (if available).
Detailed Instructions:
Step 1: Initiating a New Project
- Starting Android Studio:
Open Android Studio. If it's your first time, you'll be greeted with a welcome screen.
- Project Initialization:
Select 'New Project' from the home screen in the upper right corner situated next to 'open' and 'Get from VCS'.
In the project setup, choose "Basic Views Activity" instead of the default option. This selection provides a simple, pre-configured template with a basic user interface. However, it must be noted that depending on the version of Android Studio installed as well as the type of OS you are running, whether it be MacOS or Windows, you may encounter a different screen from the one depicted below. Though the setup is fairly similar to those across all OS'.
- Project Configuration:
Name your app (e.g., "My First App - CS 2340").
Ensure the language is set to Java, as this codelab is Java-oriented.
Leave other settings like 'Minimum SDK' at their default values for wider app compatibility.
- Finalizing Project:
Click "Finish". Android Studio then sets up the project structure, which includes generating necessary files and folders.
You'll be taken to the new Project that you just set up which includes the base structure with all the necessary files and includes
Step 2: Optimizing Your Workspace
- Initial Layout Adjustment:
The default layout might have several windows and panels. Customize this for better usability.
- Gradle Console:
If the Gradle console is visible on the right, minimize it. It's used for build processes but isn't needed immediately.
- Project Pane Management:
The Project pane on the left shows your project's file structure. Resize or collapse it to free up space for the coding area.
Step 3: Exploring Project Architecture
- Project Views:
Android Studio offers multiple views for project files, like 'Project' and 'Android' views. The 'Android' view groups files logically for Android development.
- Key Directories:
- Manifests: Contains AndroidManifest.xml, which declares app components.
- Java: Holds your Java source files.
- res (Resources): Stores resources such as layouts (layout), strings (values), and images (drawable).
Here is a more in depth understanding of what these directories are and their purpose.
- AndroidManifest.xml: This is the manifest file for your Android application. It serves several purposes, such as defining the app's package name, declaring its components (activities, services, broadcast receivers, and content providers), specifying permissions the app requires, and setting up hardware and software features your app uses. It acts as a guide for the Android system to understand your app's structure and how it should behave on a device.
- Java Directories:
- com.example.firstapp_cs2340: This directory contains the main Java source code for your app. It includes activities (screens), fragments, and other Java classes. If you're creating an app with multiple screens, each screen will be represented by a class in this directory.
- com.example.firstapp_cs2340 (androidTest): This directory is designated for Android Instrumentation Tests, which are tests that run on an Android device or emulator. These tests can interact with the UI and perform actions like clicking and swiping.
- com.example.firstapp_cs2340 (test): Here you'll write unit tests for your Java classes. Unit tests run on your development machine's JVM and are used to verify the logic of your code without the need for a device or emulator.
- res (Resources) Directory:
- drawable: Contains XML files that define shapes or state lists (selectors) and can also be used for simple graphics that your app can draw on the fly. Drawables are typically used for backgrounds, buttons, etc.
- layout: These XML files define the UI layout of your activities or fragments. For example, activity_main.xml might define the main screen, and fragment_first.xml would define the UI for a section of the screen managed by a fragment.
- menu: XML files in this directory define menus in your app, such as the options menu that appears when the user clicks the menu button.
- mipmap: This directory holds your app icons at different resolutions. Android uses the appropriate resolution icon based on the device's screen density.
- navigation: This contains navigation graphs that describe the flow from one part of your app to another. The file nav_graph.xml is a visual representation of this flow and is used with the Navigation component in Android Jetpack.
- values: This directory includes:
- colors.xml: Defines the colors used in your app's UI.
- strings.xml: Holds all the strings used in your app, which helps in localizing your app to support multiple languages.
- dimens.xml: Specifies dimensions used throughout your app's UI, like padding and font sizes.
- styles.xml: Defines the styles and themes used by your app, which can include settings for color palettes, typography, and the overall look of your UI.
- xml: This directory might contain various XML files that can be used for configuring parts of Android systems like data backup rules (backup_rules.xml) or rules for data extraction (data_extraction_rules.xml).
- Gradle Scripts:
- build.gradle.kts (Project: FirstApp_CS_2340): This is the build configuration file for your entire project and it's written using the Kotlin DSL. It includes configurations that apply to all modules in your project.
- build.gradle.kts (Module: app): This is the build configuration file for your app module. It includes dependencies for libraries your app uses, defines the Android API levels, and sets up other module-specific build settings.
Understanding these files and directories is crucial for Android app development as they form the foundation of your project structure. They help you manage resources, write and test code, and configure your app's build settings. As you progress, you'll become more familiar with these components and how they interact within your project in this tutorial.
Step 4: Creating and Configuring an Android Emulator
An Android Emulator simulates Android devices on your computer so that you can test your application without needing a physical device. This is especially useful for developers who want to test their apps on different screen sizes and Android versions.
Accessing AVD Manager
Launch AVD Manager:
In Android Studio, you can open the Android Virtual Device (AVD) Manager by going to Tools > Device Manager in the top menu. Alternatively, you can click on the Device Manager icon (it looks like a small mobile phone) in the toolbar on the right hand side.- AVD Manager Interface: The AVD Manager will show a list of your existing virtual devices if you have any. It's a centralized place to manage all your Android emulators.
Creating a New Virtual Device
- Start Device Creation: Click on the +Create Virtual Device button at the bottom of the AVD Manager. This will open the "Select Hardware" screen.
- Choose a Device: You'll see a list of pre-configured hardware profiles, such as Pixel 2, Pixel 4, Nexus 5X, and many others, each mimicking different real Android devices.
- Selection Criteria: While you can choose any device, as a beginner, it's advisable to start with a commonly used device like Pixel 2 or Pixel 4 to emulate a popular screen size and configuration.
Selecting a System Image
- System Image Selection: After selecting your hardware, you'll proceed to the "System Image" screen. Here you'll pick an Android version (also known as an API level) for the emulator.
- Recommended Tab: It's best to select the latest release under the "Recommended" tab. These images are tested by Google and should work well for most development needs.
- Downloading System Image: If the system image you want to use isn't already downloaded, you'll see a "Download" link next to it. Click the link, and a download will start. This could be quite large (several gigabytes) and may take some time depending on your internet speed.
- Disk Space: Be aware that system images consume significant disk space. Only download what you need for your current testing purposes.
- Verify and Download: Make sure to read and accept any licenses required for the system image download, then wait for the process to complete.
Configuring the Emulator
- Emulator Settings: Once the system image is installed, you'll be presented with the configuration settings for your new emulator.
- Device Name: You can give your virtual device a name that helps you identify it among other emulators. It's useful to include the Android version in the name (e.g., Pixel_3_API_34).
- Startup Orientation: Choose the orientation in which the emulator starts. You can leave this at the default setting, which is usually portrait mode.
- Scale: Adjust the scale if you want to change the size of the emulator window on your desktop.
- Memory Options: For a smoother experience, you might want to allocate more RAM to the emulator, but the default settings are usually sufficient for basic testing.
- Advanced Settings: Click on "Show Advanced Settings" if you want to customize options like VM heap size, internal storage, camera emulation, or network options.
- Finishing Setup: Review your settings, and when you're satisfied, click "Finish". Android Studio will create the virtual device based on your specifications.
Starting the Emulator
- Launching: After creating your virtual device, you can start it by selecting it from the AVD Manager and clicking on the green play button.
- First Boot: The first time you start a new emulator, it may take some time to boot up. Subsequent starts are usually quicker.

Using the Emulator
- Testing Apps: With the emulator running, you can now test your applications just as you would on a real device. Use Android Studio's "Run" button to deploy your app to the emulator.
- Simulating Interactions: The emulator allows you to simulate various actions and events, like incoming calls, text messages, device rotation, and other sensors.
Closing the AVD Manager
- After starting the emulator, you can close the AVD Manager window. Your emulator will continue to run, and you can interact with it as needed for testing your app.
Creating and using an emulator is a critical skill in Android development. It allows you to test your applications in different environments and ensure that they perform well across a variety of devices. With practice, managing and configuring emulators will become a seamless part of your development workflow.
Step 5: Running Your App on the Emulator
Introduction to Running Apps
Running your app on an emulator is a pivotal part of the development process. It allows you to see how your app will look and behave in a simulated Android environment. This step assumes you've already built a simple app that displays a "Hello World" message.
Starting the Emulator
- Launch the Emulator: Before you can run your app, you need to start the emulator.
- Open Android Studio and locate the AVD Manager.
- Start the emulator you created in the previous steps by clicking the green play button next to it.
- Wait for the emulator to boot up. This may take a few minutes, especially if it's the first time starting this particular emulator.

Running Your App
- Initiate the Run Command: In Android Studio's main menu, click on Run > Run 'app' or click the green triangle (Run icon) on the toolbar.
- Select the Deployment Target: A window titled "Select Deployment Target" will appear, listing all available emulators and connected devices.
- Choose the emulator you just started from the list.
- Click "OK" to deploy your app to the emulator.
Monitoring the Launch Process
- Watch the Status Bar: As your app is building and deploying, you can monitor the progress in the status bar at the bottom of the Android Studio window.
- Common Messages: You'll see messages such as "Gradle build running," "Waiting for target device to come online," "Installing APK," and "Launching activity." These indicate that Android Studio is compiling your code, preparing the emulator, installing your app, and launching it, respectively.
Interacting with Your App
- App Display: Once the emulator is ready and your app is installed, it should automatically open to display your "Hello World" message.
- Interact with Your App: You can now interact with your app as if it were running on a real device. Try clicking buttons, entering text, or using other UI elements you've created.
Best Practices
- Starting the Emulator Early: It's a good practice to start your emulator at the beginning of your development session to avoid the wait time for booting up every time you run your app.
- Keeping the Emulator Open: Don't close the emulator until you're done testing to streamline your workflow.
- One Emulator at a Time: Avoid running multiple emulators simultaneously to conserve system resources and improve performance.
Troubleshooting
- Instant Run: If you encounter a dialog box stating "Instant Run requires that the platform corresponding to your target device (Android N...) is installed," click "Install and continue." This feature allows for quicker deployments of incremental changes to your app.
- Emulator Issues: If the emulator doesn't start or your app isn't running as expected, try the following:
- Close the emulator and restart it.
- Clean your project by going to Build > Clean Project.
- Rebuild your project with Build > Rebuild Project
- Ensure that Android Studio is set to use the correct build variant for your app.
Step 6: Running on a Physical Device (Optional)
Introduction to Physical Device Testing
Testing your app on a physical device is an invaluable part of the development process because it gives you a clear insight into how your app behaves in a real-world scenario. This step will guide you through the process of setting up your Android device and running your app on it if you choose to do so.
Device Preparation
- Gather Requirements:
- You'll need an Android device, like a smartphone or tablet, and a USB cable compatible with your device to connect it to your computer.
- Install USB Drivers (Windows/Linux):
- For Windows, you might need to install specific USB drivers for your Android device. You can typically find these on the device manufacturer's website under their support section.
- Linux may require udev rules to be set up to recognize the device.
Enabling USB Debugging on Your Android Device
- Developer Options:
- Go to Settings > About phone on your device.
- Tap Build number seven times to unlock Developer options. You might need to enter your device's PIN or password.
- Enable USB Debugging:
- Return to the main settings screen, and you should see Developer options at the bottom.
- In Developer options, find and enable USB Debugging.
Running the App on Your Device
- Connect Your Device:
- With USB Debugging enabled, connect your Android device to your computer using the USB cable.
- Allow USB Debugging:
- A prompt may appear on your device asking you to allow USB debugging. Tap OK to permit your computer to communicate with your device.
- Run the App from Android Studio:
- Click Run (represented by the green play icon) in the toolbar of Android Studio.
- In the "Select Deployment Target" dialog, you should see your device listed under available devices. Select it and click OK.
- App Installation and Launch:
- Android Studio will install your app on the device and launch it automatically. You can then interact with your app as you would under normal use.
Troubleshooting Device Connection
- Device Not Recognized:
- Try a different USB port or cable to ensure there's no physical connection issue.
- Restart both your development machine and the Android device to reset the connection.
- Device Unauthorized:
- If your device is still not recognized or shows as unauthorized, you may need to revoke USB debugging authorizations.
- Go to Settings > Developer options on your device and tap Revoke USB Debugging authorizations.
- Reconnect your device and accept the USB debugging permission prompt.
- Driver Issues (Windows):
- If you're on Windows and your device is still not recognized, ensure that the correct USB drivers are installed. Without the right drivers, your device might not be listed in the "Select Deployment Target" dialog.
- Using Hardware Devices Documentation:
- For further troubleshooting, refer to the Android documentation on running apps on a hardware device.
- Android Studio Troubleshooting Section:
- If issues persist, check the Android Studio documentation's troubleshooting section for additional solutions.
Step 7: Navigating the Basic Activity Template
When you start a new project in Android Studio and select the Basic Activity template, the IDE automatically generates a number of files and resources for you. This template provides a good starting point and is designed to help you quickly start developing your app with a clean, simple interface.
Exploring the User Interface Elements
- Floating Action Button (FAB):
- The FAB is typically used for a primary action in your app, such as composing a new email or adding a new item. It's represented by a circular button that floats above your app's UI, often at the bottom right corner.
- In the generated project, the FAB comes with an email icon. When you tap this button, it's set up to show a Snackbar, which is a brief message that appears at the bottom of the screen.
- Snackbar:
- A Snackbar provides lightweight feedback about an operation. It shows a message at the bottom of the screen for a short duration and can include an action for the user to respond to, like undoing an action they just performed.
- It's a useful UI component for showing quick notifications without interrupting the user's flow.
- Settings Menu:
- The options menu, accessible via the three vertical dots icon in the top-right corner of the screen, is pre-configured with a "Settings" item.
- While selecting "Settings" doesn't perform any action by default, the menu is set up so you can easily implement settings or preferences for your app.
Understanding the App's Structure
- Activity Files:
- The Basic Activity template includes a main activity file (MainActivity.java or MainActivity.kt), which serves as the entry point for the app.
- It also includes layout files for the activity (activity_main.xml), which define the UI structure, and content files (content_main.xml) where you can start adding more complex UI components.
- Fragment Files:
- Fragments (FirstFragment and SecondFragment) are modular sections of an activity that you can reuse within different activities. They have their own layout (fragment_first.xml and fragment_second.xml) and lifecycle methods.
- Navigation Graph:
- The navigation graph (nav_graph.xml) visually represents the navigation flow between different fragments of the app. It's part of the Navigation component in Android Jetpack, which simplifies implementing navigation.
- Resource Directories:
- Drawable resources for images and shapes used in the UI.
- The layout directory for UI layouts.
- Values for strings, dimensions, colors, and styles that define the look and feel of your app.
Customizing the Template
- Modifying Layouts:
- You can start customizing your app by modifying the XML layout files. For instance, you can change the text on the FAB or add new UI components to content_main.xml.
- Adding Functionality:
- To make the "Settings" menu functional, you'll typically add a new SettingsActivity and link it to the menu item. You can then populate the SettingsActivity with preferences that allow users to modify aspects of your app's behavior.
Best Practices
- Familiarize with the Codebase:
- Spend time exploring and understanding the generated code and resources. It will give you a better understanding of Android app structure and conventions.
- Build Gradually:
- Start by making small changes and testing them. Gradually build up the complexity of your app by adding new features one at a time.
This comprehensive guide covers everything from setting up your first Android project in Android Studio, configuring the workspace, understanding the project's architecture, to running your app on an emulator and a physical device. It provides insights into the structure and features of the Android Studio environment, ensuring a solid foundation for your Android app development journey. You’ll continue on with this guide for a more in depth understanding of Android Studio and building your first app.