Creating Your First Android App
1. Setting Up the Development Environment
Before you start coding, you need to set up your Android development environment.a. Installing Android Studio
‣ Android Studio is the official integrated development environment (IDE) for Android development.
‣ Download and install Android Studio from the official website: https://developer.android.com/studio.
‣ During installation, make sure to install the necessary SDKs and virtual devices (AVDs) for testing your app on different screen sizes and devices.
b. Setting up the Emulator
‣ Android Studio provides an emulator to simulate Android devices on your computer. You can configure and test your app on different virtual devices.
‣ Alternatively, you can use a physical device for testing, but for simplicity, we will use the emulator in this chapter.
2. Creating a New Project
Once Android Studio is installed and set up, follow these steps to create a new project:a. Start a New Android Project
‣ Open Android Studio and select Start a new Android Studio project.
‣ Choose a template for your app. For beginners, the "Empty Activity" template is a great choice, as it provides a clean slate to start with.
‣ Name your project (e.g., "MyFirstApp") and set the language to Java or Kotlin (Kotlin is the preferred language for Android development).
‣ Choose the API level for your app (selecting the minimum API level your app will support).
‣ Click Finish to create the project.
3. Understanding the Project Structure
Your Android project will consist of several folders and files. Here’s an overview of the important ones:a. Java/Kotlin File
(MainActivity.java or MainActivity.kt)
‣ This file contains the logic of your app.
‣ The MainActivity class is the entry point of your app. The code here handles user interactions and manages app behavior.
b. XML Layout File
(activity_main.xml)
‣ The layout XML file defines the UI of your app. It is where you design your app’s screen using a combination of UI components (buttons, text fields, etc.).
‣ This file is located in res/layout/.
c. Manifest File
(AndroidManifest.xml)
The manifest file contains essential information about your app, such as its components (activities, services), permissions, and other settings.
4. Designing the User Interface
a. Using the Layout Editor‣ Android Studio provides a layout editor to visually design your app’s interface. You can switch between Design and Code view to create your UI.
‣ In the activity_main.xml file, you can add various UI elements such as:
◙ Button: A clickable button.
◙ TextView: Displays text on the screen.
◙ EditText: Allows user input.
Example XML layout for a simple app with a button and text view:
5. Writing the Code
a. Adding Logic in MainActivity.java or MainActivity.ktOnce the UI is designed, you need to add functionality to the app. For instance, you want to display a message when the user clicks the button.
Here’s an example of how to do this in Java
In Kotlin :-
6. Running Your App
Once you’ve written the code, you can test your app using the Android Emulator or a physical device.a. Using the Emulator
‣ Click on the Run button (green triangle) in Android Studio to launch the app in the emulator.
‣ Select a virtual device or create a new one if you haven't already.
b. Using a Physical Device
‣ Enable Developer Options and USB Debugging on your Android phone.
‣ Connect the device to your computer via USB and select it from the list of available devices in Android Studio.