Sunday, January 19, 2020

Creating and running first Android App

Build your first Android app

This section describes how to build a simple Android app. First, you learn how to create a "Hello, World!" project with Android Studio and run it. Then, you create a new interface for the app that takes user input and switches to a new screen in the app to display it. 
Before you start, there are two fundamental concepts that you need to understand about Android apps: how they provide multiple entry points, and how they adapt to different devices and screens.

Apps provide multiple entry points to execute.

Android apps are built as a combination of components that can be invoked individually. For example, an activity is a type of app component that provides a user interface (UI). The "main" activity starts when the user taps your app's icon. You can also direct the user to activity from elsewhere, such as from a notification or even from a different app.
Other components, such as broadcast receivers and services, allow your app to perform background tasks without a UI.
After you build your first app, you can learn more about the other app components at Application fundamentals.

Apps adapt to different devices accordingly.

Android allows you to provide different resources for different devices. For example, you can create different layouts for different screen sizes. The system determines which layout to use based on the screen size of the current device.

If any of your app's features need specific hardware, such as a camera, you can query at run time whether the device has that hardware or not, and then disable the corresponding features if it doesn't. You can specify that your app requires certain hardware so that Google Play won't allow the app to be installed on devices without them.

Creating an Android project

This lesson shows you how to create a new The android project with Android Studio, and it describes some of the files in the project.

To create your new Android project, follow these steps:
  1. Install the latest version of AndroidStudio.
  2. In the Welcome to Android Studio window, click Start a new Android Studio project.
  3. In the Choose your project window, select Empty Activity and click Next.
  4. In the Configure your project window, complete the following:
  • Enter "My First App" in the Name field.
  • Enter "com.example.myfirstapp" in the Package name field.
  • If you'd like to place the project in a different folder, change its Save location.
  • Select either Java or Kotlin from the Language drop-down menu.
  • Select the checkbox next to Use androidx.* artifacts.
  • Leave the other options as they are
       5 Click Finish.

After some processing time, the Android Studio main window appears.

Now take a moment to review the most important files.

First, be sure the Project window is open (select View > Tool Windows > Project) and the Android view is selected from the drop-down list at the top of that window. You can then see the following files:

app > java > com.example.myfirstapp > MainActivity

This is the main activity. It's the entry point for your app. When you build and run your app, the system launches an instance of this Activity and loads its layout.

app > res > layout > activity_main.xml

This XML file defines the layout for the activity's user interface (UI). It contains a TextView element with the text "Hello, World!"

app > manifests > AndroidManifest.xml

The manifest file describes the fundamental characteristics of the app and defines each of its components.
Gradle Scripts > build.gradle

There are two files with this name: one for the project, "Project: My First App," and one for the app module, "Module: app." Each module has its own build.gradle file, but this project currently has just one module.

Run-on a real device

Set up your first device as follows:
  1. Connect your device to your development machine with a USB cable. If you developed on Windows, you might need to install the appropriate USB driver for your device.
  2. Perform the following steps to enable USB debugging in the Developer options window:
  • Open the Settings app.
  • If your device uses Android v8.0 or higher, select System. Otherwise, proceed to the next step.
  • Scroll to the bottom and select About phone.
  • Scroll to the bottom and tap Build number seven times.
  • Return to the previous screen, scroll to the bottom, and tap Developer options.
  • In the Developer options window, scroll down to find and enable USB debugging.

Run the app on your device as follows:

  1. In Android Studio, select your app from the run/debug configurations drop-down menu in the toolbar.
  2. In the toolbar, select the device that you want to run your app on from the target device drop-down menu.