After you have successfully completed the Android Studio Installation , the next experiment is to test whether everything is running well, the way we create and run the "Hello World" project. Please follow these steps:
This tutorial is in the name of Wawan Chahyo Nugroho (NIM: 12131294), created in order to fulfill Assignment 2 Mobile Programming, which is supervised by Mr. Untung Subagyo, S.Kom.
If Android Studio is opened for the first time, a dialog about license agreement will appear, you may need to accept the agreement and just use the default settings.
1. Open Android Studio
2. Create Your Project Name
Create an Android Studio project name according to your taste or needs.
3. Determine the Target Android Device
Please determine the things that can affect the running of your application, such as which platform is suitable for running your application, different platforms may also require separate SDKs.
Lower level APIs (application programming interfaces) can be used to target more platforms, but provide fewer features.
Defining Target Android Device - As a first project, please use the default rules
4. Adding Activity Templates
Adding an Activity Template - As a starting project please select Empty Activity
5. Customize Activities
At this stage you are given the opportunity to change the Activity name and the Layout name. This naming will represent the class name of your project.
Customize Activity - As a starter project, just use the default settings.
6. Getting to know the Android Studio Workspace
After you go through the project creation preparation stage, next you will be shown the Design view with a widget palette and preview images that look like an Android device. Allows you to develop applications by Drag & Drop in Design mode (GUI) or by coding in Text mode (CLI/Console).
Getting to Know the Android Studio Workspace
Play Button Icon
Click the "Play" button on the top toolbar to run your application. A dialog box will appear asking you which Virtual Device you want to use. If you haven't created one yet, click "Create New Virtual Device".
7. Create an Android Virtual Device (AVD)
Virtual device availability dialog
As a first step, just follow the default settings, namely "Nexus 5 API 21 x86" as a Virtual Device that is suitable for you as a beginner, click Next.
Next, the system image page for a particular API level will be displayed. As a first step in learning, please open the Recommended tab, follow the default settings, then click Next.
System Image - Virtual Device Configuration
If you see the Download list in System Image, it is important to know the API level of the target device before you download it, because your app will not run on a system image with a lower API level, as specified in the minSdkVersion attribute of your app's manifest file. For more information, see Versioning Your App .
Next, the Verify Configuration page will be displayed .
Verify AVD Configuration page
Please if there are AVD properties you want to change. For example in this setting I got a recommendation to install HAXM, the red highlight seems to indicate something I must do.
After I clicked install HAXM, next I was presented with the Emulator Settings page.
Emulator Settings Dialog
The dialog says that "We have detected that your system is able to run the Android emulator in Performance Acceleration (Speed) mode".
Sets the available RAM allocation for Intel® Hardware Accelerated Execution Manager (HAXM) to Maximum, applicable to all x86 emulators. You can also change this setting at any time by running the Intel® HAXM installer. In this case I use the default/recommended settings, and click Next.
Next, wait until the download and installation process is complete >> Click Finish >> and click Finish on the Verify Configuration page .
Now the emulator is available and ready to use, click OK.
Nexus 5 API 25 - Android Virtual Devices
At this stage (Creating and Using Android Virtual Device), I encountered an obstacle, where the AVD that I created based on the default settings / system recommendations could not run smoothly, because the 1GB RAM allocation seemed too large, so that the need could not be met by the Physical Memory that I had (4GB installed, and only 1.5GB free left)
And when Android Studio is run together with AVD, then the 1GB AVD allocation makes it impossible to run on my laptop.
Long story short, I created another AVD with specifications to suit my laptop's performance.
Regarding this "AVD Preparation", God willing, I will review it specifically on a special page on a future occasion.
8. Running Applications With AVD
After I created another alternative virtual device with the specifications shown by the Nexus S.
Creating an alternative AVD
When Android Studio is run simultaneously with Nexus S AVD, you have to sacrifice almost all resources (RAM), and be patient not to open any applications other than Android Studio.
Android Studio + ADV = RAM hog
However, with much patience, I finally managed to get my application running on the AVD.
Running Apps on Android Virtual Device
9. Understanding Project Structure
Before starting development, I will first try to explain some of the structures in our project, including:
manifests:
Describes the nature of the application and each of its components.
Every application must have an AndroidManifest.xml file (named exactly like this) in the root directory. The manifest file provides important information about the application to the Android system, so that it can run each application's code.
What the manifest file does includes:
- Names the Java package for the application. The package name serves as a unique identifier for the application.
- Describes the various components of an application, including the activities, services, broadcast receivers, and content providers that make up the application. It also names the classes that implement each component and publishes their capabilities, such as the Intent messages they can handle. These declarations inform the Android system about the components and the conditions under which they can be launched.
- Determines the process that hosts the application components.
- Declares the application permissions that the application must have to access protected parts of the API and interact with other applications. Also declares other permissions that the application must have to interact with application components.
- Lists the Instrumentation classes that provide profiles and other information while the application is running. This declaration is only present in the manifest when the application is built and is removed before the application is published.
- Declares the minimum Android API level required by the application.
- Lists the libraries that the application should link to.
java:
Contains *java files as source code, separated by package names, including JUnit test code. This section is likened to the controller in the MVC concept.
res:
Contains code but not as source code but design code such as XML layouts, UI strings, and bitmap images, divided based on their respective sub-directories. The components here are likened to Assets or Views in the MVC concept. More information about the types of resources that can be accommodated by this structure can be seen HERE.
10. Printing Text with Buttons
Go to app → res → layout → activity_main.xml
Next, drag & drop the Button widget into the layout design, so that it looks like this:
Using Button Widget
Next open the MainActivity.java tab and add the following code:
Modify MainActivity.java Code
Information:
- Import the required libraries (you can also do this automatically when you write commands inside the function by pressing ALT+Enter).
- Implement the View.onClicListener method in the main class (you can also do this automatically when you write the setOnClickListener() method in the function by pressing ALT+Enter).
- Declaring the required variables
- Define the onCreate() method by referencing a variable based on the previously created widget ID, and setting the string to Rewrite the existing one ("Hello World").
- Define the onClick() method to change the existing setText to the contents of the hello variable.
Next run your application in the emulator/AVD:
Before the Button is Clicked
After the button is clicked
Reference:
- https://web.stanford.edu/class/cs193a/android-studio.shtml
- https://developer.android.com/studio/run/managing-avds.html
- https://developer.android.com/guide/topics/manifest/manifest-intro.html
- http://stackoverflow.com/questions/9192109/displaying-a-string-on-the-textview-when-clicking-a-button-in-android