Creating Android Applications Android Studio
Android Studio is an IntelliJ IDEA-based integrated development environment (IDE), which Google calls the official IDE for Android applications.
This guide describes android application development:
- Navigate between files using Explorer
- Install AndroidManifest.xml
- Import files into a project
- Advanced layout editor with dynamic preview function
- Using Logcat and Android Monitor to Debug Applications
Getting started in Android Studio
Launch Android Studio in the window Android Studio Setup Wizard select Start a new Android Studio project (start a new project).
In the window Create new project select Application name (application name) as Fortune ball, enter the company domain; in field Project location Select the location where the application will be saved. Click on Next.
Before you window Target Android Devices. Select Phone and Tablet. In field Minimum SDK indicate API 15. Click Next.
In the window Add an activity to Mobile select Basic activity. Rate all options, this window provides an overview of the available layouts.
In the window Customize the activity, a screenshot of which is posted below, you can change Activity name (name of activity) Layout name (layout name) Title (common name) and Menu Resource Name (name of the resource menu). Leave the defaults and click. Finish.
After a few seconds, the following window will appear:
The same window will appear on your device or emulator. The emulator functions as a device, loading will take some time.
This is an application. He lacks a lot, but now it is already possible to move on to the next step.
Project and file structure
The window shows the project files.
In the drop-down menu (screenshot below) there are several filters for files. The main ones are Project and Android.
The Project filter will show all application modules. at least one module is present in each project. Other types of modules include modules from third-party libraries, or modules of other Android applications (such as applications for Android Wear, Android TV). Each of the modules has its own set of characteristics, including a gradle file, resources and source files (java files).
Note. If the project is not open, click the Project tab on the left side of the panel, as shown in the screenshot. By default, the Android filter is installed, which groups files by a specific type. At the top level, you will see the following folders:
The following sections detail all of these folders, starting with manifests.
Overview of AndroidManifest.xml
Each Android application has an AndroidManifest.xml file, which is located in the manifests folder. This XML file tells your system about the requirements of the application. The presence of this file is mandatory, because it is it that allows the Android system to create an application.
Open the manifests folder and AndroidManifest.xml. Double click to open the file.
The tags manifest and application are needed for manifest; they appear only once.
Each tag also defines a set of attributes, along with the name of the element. For example, some attributes in application may be:
android: icon, android: label and android: theme
Among other things, in manifest may appear:
- uses-permission: requests a special permission, which is issued to the application for correct operation. For example, the application must request permission from the user in order to gain access to the network. in case you add the android.permission.INTERNET permission.
- activity: reports an activity that is partially responsible for the visual UI and logic. Any activity that is provided in the application must be added to the manifest. the system will not notice the unmarked activity, and it will not be displayed in the application.
- service: adds the service that you are going to use to implement long-running operations or advanced API communications with other applications. An example in this case is a network call with which the application receives data. Unlike activities, services do not have user interfaces.
- receiver: thanks to the receiver of broadcast messages, the application receives signals about system messages or messages from other applications, even when other components of the application are not running. An example of such a situation is a battery with a low charge level and a notification about this by the operating system.
A complete list of tags can be found in the manifest file on Android Developer.
Manifest File Settings
In activity, add the following attribute:
android: screenOrientation = ”portrait”. to limit the screen to portrait mode only. If this is not done, the screen, depending on the location of the device, will be either in landscape or portrait mode. After adding the attribute, the manifest file will look like in the screenshot.
Build and run the application. If you are testing on your device, turn it over, make sure that the screen does not move to landscape mode if you have limited this feature in the AndroidManifest file.
Let’s move on to Gradle. Gradle turns an Android project into an installable APK that can be installed on devices. The build.gradle file is present in Gradle scripts, at two levels: modular and design.
Open the build.gradle file (Module: app). You will see the default gradle setting:
Let’s analyze the main components:
- apply plugin: ‘com.android.application’ applies the Android plugin at the parent level and ensures the availability of top-level tasks that are needed to create the application.
- Near the android section .settings options such as targetSdkVersion are found. The target SDK for your application must be at the latest API level. Another important component is minSDKVersion (defines the minimum version of the SDK that must be installed on the device for the application to run). For example, if the SDK version is 14, then the application will not be able to run on this device, since in this particular case the minimum supported version is 15.
- The last component is dependencies.. It should be noted compile ‘com.android.support:appcompat-v7:VERSION’ and compile ‘com.android.support:design:VERSION’. They provide support and compatibility features of the new and old APIs.
In addition to the Android compatibility libraries, you can add third-party libraries to the dependencies component .An animation library, for example, contains user interface effects. Find the dependencies, then add the following two lines down:
Here you can add third-party dependencies. Libraries are automatically downloaded and integrated into Android Studio. Click Sync Now to integrate these dependencies into your application.
Synchronization lasts a few seconds. Gradle updates appear in the Messages tab of the bottom pane.
These are all the settings that you will need in Gradle for now. Such manipulations will add animation to the application.
In developing an application for Android, integration with other resources is important: images, custom fonts, sounds,s, etc. These resources are imported into Android Studio and placed in the appropriate folders, which allows the operating system to select the correct resources for the application. For our Fortune Ball application, we will need to import images into drawable folders. These folders may contain images or special XML drawables (that is, you can draw forms using XML code and use them in your layouts).
Download the images from the link, then unpack the components and place them in a convenient place.
In Android Studio, go from Android to Project
Open the res folder (app src main). Right click on the res folder, select New Android resource directory.
A window called New Resource Directory will appear.
From the Resource type drop-down list, select the drawable option. In the Available qualifiers list, select Density, then click the button that is highlighted in the screenshot.
In the next window, select XX-High Density from the Density list. Click OK.
Repeat all to create the drawable-xhdpi, drawable-hdpi and drawable-mdpi folders. Select X-High, high and medium density respectively from the Density list.
Each folder that has a density determinant (i.e. xxhdpi, xhdpi, hdpi) contains images that correlate with a specific density or resolution. For example, the drawable-xxhdpi folder contains a high-density image, which means that an Android device with a high-resolution screen will extract the image from this folder. The image will look good on all Android devices, regardless of screen quality. For more information on screen density, see the Android documentation.
Having created all the “drawn” folders, you can return to the unpacked content in the folder and copy (cmd C) the image from each folder and place (cmd V) in the corresponding Android Studio folder.
Having placed the files, you will see the Copy window. Select OK.
XML View and Dynamic Layout Previews
Creating a layout that users can interact with is an important part of the process. In Android Studio, this can be done in the layout editor. Open content_main.xml from res / layout. In the Design tab, you can transfer interface elements (buttons, text fields).
To the right of Design there is a Text tab that allows you to edit XML directly in the layout.
Before creating an appearance, you need to define some values. Open strings.xml in the res / values tab and add the following:
strings.xml contains all the lines that appear in the application. Separation of these lines into separate files simplifies internationalization, as only a line file is needed for each language that is required in the application. Even if you are not going to translate the application into other languages, using a string file is always recommended.
Open the dimensions.xml in res / values and add the following:
dimensional.xml contains dimensional values, marginal intervals for layouts, text size, etc. It is recommended that you save this data in a file so that you can use it to create layouts in the future.
Return to content_main.xml and replace the entire contents of the file with the following code:
This rather large code creates a mock application called FortuneBall. At the top level, you added RelativeLayout (relative markup determines the position of the child components relative to the parent component). RelativeLayout can be stretched to fit the size of the parent component.
In relative markup, two pieces of text are added, an image and a button. All these details will be visible in the order they are added. Their contents can be read in strings.xml (text) and in drawable (images).
When updating content_main.xml, note the Preview window that updates the UI:
Note: if the preview window is not visible, in the Text tab, click on the Preview button in the markup editor panel on the right.
Create and run.
And so you have created the application layout. But at this stage it is only a beautiful image. clicking on a button will not lead to anything.
Combine Activity and View
You can use java files hosted in app / src / main / java to establish logical connections in the application.
Open MainActivity.java and add this data under existing ones:
The first five imports point to the corresponding classes in your code: Random, View, Button, ImageView and TextView. The following two imports indicate that you will be using two classes from libraries, including build.gradle for animations. In MainActivity.java, in the MainActivity class, add:
In this small piece of code, you set 4 variables for the activity. The first is the lines that determine the possible states, the other three are the UI elements that you created in the layout / layout.
Now replace the contents of the onCreate () method with the following data:
- Verify that the activity is ready (superclass implementation).
- Indicate that the layout for this activity is represented by the layout that you created earlier, check the toolbar.
- Fill in the values of the three variables that you created earlier in the views components of the layout using the findViewById method. The id value is the same as in the XML layout.
- Add OnClickListener on the button. This is a simple class that encapsulates (packs) functionality, which in turn is triggered by a button click.
- Select an arbitrary option from the fortuneList for this application, and refresh the fortune text to show this.
- Use a third-party library to add the dependency to the gradle file, and thus add animation to the application.
Already almost done. But you need to remove the floating button. Go to res / layout and open activity_main.xml.
This layout file contains a link to the content_main.xml that you previously edited. It defines content by default (toolbar and floating action button). However, in this particular application (Fortune Ball) a floating button is not needed. Therefore, remove the following block of code from the xml file:
Now in the lower right corner there is no floating button.
Ask a question (What’s my fortune?). click the button. Check the application.
Android Studio has a wide variety of tools. Click the Android Monitor tab at the bottom of the Android Studio window.
Here you will find many options for the developer.
- The camera and the play button on the left allow you to take screenshots and record.
- A magnifying glass opens up a number of additional options, such as analyzing application memory.
- Layout Inspector provides a visual interface that determines why the application interface looks a certain way.
LogCat provides a detailed overview of system messages with the ability to delve into the data of a specific application, or even use the search bar to filter messages if they do not contain specific characters.
Check that you selected Show only selected application in the upper right corner, as shown in the screenshot above. Now only messages from your application will be visible.
In MainActivity.java, add the following to the list of imports:
At the end of onCreate () in MainActivity.java add the following line:
Log.v calls two parameters. a tag and a message. In this case, the tag is defined as “FORTUNE APP TAG”, and the message as “onCreateCalled”.
Run the application to see the log message in the Logcat panel.
Filter the contents of LogCat, type onCreateCalled into the search bar above the console:
Then delete the search text to see all the log messages again.
Another useful feature is logcat, which is the ability to view error messages. Add a bug to your fully functional application to see how it works.
Go to MainActivity.java and change the following line in onCreate ():
Launch the app. Click the What’s My Fortune button? Does not work!
How would you fix the error if you did not know about the presence of a bug? This will help Logcat.
Go to the Logcat panel. it looks something like this: