How to create your application on Android
How to create your application on Android
To write the program code for the application "Android" a special development environment is used. Since the creation of the operating system for programming, Eclipse, and IntelliJ IDEA products have been used. To do this, plug-ins have been released, specifically designed to create applications for Android. However, after the release of the official development environment from Google - Android Studio plugins have ceased to be supported, and programming occurs in this software package. In order for Android Studio to work on the system, it must have a Java Development Kit, a free development kit that includes the Java code compiler, the library, and the JRE runtime. Besides to the JDK, you will need to download the Android SDK, a development kit for Android, that contains a set of tests and debugging applications.
First steps
Before you create your application, Android Studio will offer several options for the development of events. It is necessary to choose the first one - to create a new project for Android Studio.
Next, the program will ask you to specify the name of the future application, the company's domain and the physical location of the project on the computer's disk. In the next window, you will need to enter the minimum version of Android on which the application can run. If you select different options, the description below will indicate which percentage of devices with the current settings are registered on Google Play. This allows you to choose the largest audience for your product. As everyone can create new applications, the competition in this sphere is huge. So, the more coverage, the better.
In the next step, the system will present several standard markup schemes for the application. They are responsible for the location of the components on the screen. The easiest option for creating an application for "Android" is empty markup. After clicking on the Next button, the program will transfer the developer to the code editor.
Anatomy of programming for "Android"
To create an application yourself, you need to understand the structure of the project. In the left window, there is a hierarchy of elements of the future program. The root section of the app contains all the subdirectories of the application. Ready-made plan for advertising ICO Everything you need for a successful ICO.
The manifests folder contains the main file of any program for "Android" - AndroidManifest. It carries a lot of technical information for the application. In most cases, you do not have to edit it, because the SDK will generate the necessary strings and their parameters. The java folder contains the main source code. Here are the files with the extension .java. In them, and most of the development will take place. Several subfolders with resources are defined in the res folder. The drawable subdirectory is intended for graphics files required for the visual design of the project. The layout contains files with the extension .xml, which determine the characteristics and location of the elements on the screen - buttons, text boxes, switches, etc. Inside the mipmap, icons for the application of several resolutions are copied for optimization on different diagonals of screens. It is from this folder that an image will be taken to create an icon on the gadget's desktop. The values directory defines the constant values that are needed for the application. For example, color codes, lines for encountered buttons and descriptions, and style characteristics. The Gradle Scripts section was created to store the service information. It is needed for the Gradle project assembly system, which will collect and compile the project based on the entered parameters.
Code
Before you create an application, you must complete a course of at least superficial training of API and language. Also here, you will need to pre-tighten knowledge of Java. The main class, created by default, is called MainActivity.java. It will start downloading the application on the device. Android Studio itself generated the code necessary for the first run.
Classes and methods
The class itself starts with the specification of public access. It serves to differentiate the rights to use classes. The word public says MainActivity.java open and can be used from other parts of the project. Next comes the keyword, his name and the most interesting is expanding. This term means that the current class inherited from Activity. So he gets all their methods and fields. The body of the class enclosed in curly brackets. Inside them, there are methods and fields that make up the structure of all the code. @Override is an annotation, warning Wednesday that the next method for it to be overridden. This means that you need to take it from the parent class and add unique changes to the code. Thus, the mechanism of inheritance works in Java. The abstract uses the protected access specifier, which means that this method can be accessed in this class, and from the heirs, and nowhere else. The keyword void indicates the type of the method, or rather that he should return in the result. This type says that nothing is returned and only does what is in the body. Methods can have types String, int, double, and others, depending on the values. The name of the OnCreate is the name of the overridden method from the parent Activity class. To show the system implementation, which is called exactly the super keyword with a call to OnCreate is added into the body. The following line is not less interesting. The method setContentView () "wring" class MainActivity.java in the markup file, telling him how it should look like the screen of this activity.
Display
For what elements will be located on the screen, answer files markup. They contain strings with parameters that specify constant or variable values. This markup language is made according to the XML specification. Thus, the format of the parameter declaration is standardized. To create an application for "Android", you need a little understanding of the markup. Android Studio itself generated a simple file.
Elements of the screen
The root markup container in this case - RelativeLayout says that all elements inside it will be placed. It contains two parameters - android: layout_width = "match_parent" and android: layout_height = "match_parent". The first is the width of the RelativeLayout container, and the second is its height. The match_parent constant says that both indicators must match the parent, and so the element will unfold in the full screen.
Then, in RelativeLayout, a TextView is embedded, which is nothing more than an ordinary inscription. It has familiar layout_width and layout_height parameters, whose attribute is equal to wrap_content, which means - around the content. That is, the dimensions of the element will be adjusted to the volume of the text. The next two parameters mean the alignment of the text inside the element. The padding parameter takes a numeric value to create an indentation in the caption from the resource folder value. The most important parameter for an inscription is a line with text. It can be written into the attribute, but this is called hardcode and is considered unacceptable since it can complicate the localization of the application. It is worth noting that the data to be displayed, the text string, is also taken from the strings.xml file, which is in the values folder.
The first launch
The standard Android device emulator "Android" requires supernatural performance. You can run the application on it, or download and install an alternative analog, called Genymotion.
The application is launched by clicking the green Run button on the Android Studio control panel. If no errors are found when assembling the project, then the application will start and greet the world.