We saw in the previous chapter how to create a first project on Android Studio. But we did not talk about the files and directories that make up the project. Today we will describe in detail the structure of an android project.

Before running your application, you should be aware of some directories and files in the Android project:

Java

This contains the .java source files for your project. By default, it includes a MainActivity.java source file that has an activity class that runs when your application is launched using the application icon.

res / drawable-hdpi

It is a directory for drawing objects designed for high-density screens.

res / layout

This is a directory for files that define the user interface of your application.

res / values

This is a directory for other XML files that contain a collection of resources, such as string and color definitions.

AndroidManifest.xml

It is the manifest file that describes the fundamental characteristics of the application and defines each of its components.

build.gradle

This is an automatically generated file that contains compileSdkVersion, buildToolsVersion, applicationId, minSdkVersion, targetSdkVersion, versionCode, and versionName.
The following section will give a brief overview of some of the important application files.

The main activity file

The primary activity code is a Java MainActivity.java file. This is the actual application file that is eventually converted to a Dalvik executable and runs your application. Here is the default code generated by the application wizard for Hello World! :

    
package com.example.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Here, R.layout.activity_main refers to the activity_main.xml file located in the res / layout folder. The onCreate () method is one of many methods imagined when an activity is loaded.

The Manifest file

Regardless of which component you are developing as part of your application, you must declare all its components in a manifest.xml file that resides at the root of the application project directory. This file works as an interface between the Android operating system and your application, so if you do not declare your component in this file, it will not be considered by the operating system. For example, a default manifest file will look like a following file:
   

    















.
<Application> ... </ application> label components related to the application. Android attribute: the icon will indicate the application icon available under res / drawable-hdpi. The application uses the image named ic_launcher.png located in the drawing foldersThe <activity> tag is used to specify an activity and the attribute android: name specifies the fully qualified class name of the Activity subclass and the: android: tag attributes specify a string to use as the label for the activity 'activity. You can specify multiple activities using the <activity> tags.The action for the intent filter is called android.intent.action.MAIN to indicate that this activity serves as an entry point for the application. The intention-filter category is called android.intent.category.LAUNCHER to indicate that the application can be launched from the launcher icon of the device.The @string refers to the strings.xml file explained below. Therefore, @ string / app_name refers to the string app_name defined in the strings.xml file, which is "HelloWorld". Similarly, other chains are populated in the application.Here is the list of labels you will use in your manifest file to specify different Android application components -<Activity> elements for activities<Service> service items<Receiver> for broadcast receivers<Provider> elements for content providers

The strings.xml file

The strings.xml file is located in the res / values ​​folder and contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar string types enter this file. This file is responsible for their textual content. For example, a default string file will look like this:

The Activity_main.xml file (Layout)

Activity_main.xml is a layout file available in res / layout directory, referenced by your application when building its interface. You will modify this file very frequently to modify the layout of your application. For your "Hello World!" Application, this file will have the following content related to the default layout.
In order to see the message "Hello World!" When running the application, you must add the following code to the main Java file MainActivity.java:


        TextView tv = new TextView (this);
Tv.setText ("Hello, World!");
setContentView (tv);


You must also import packages: android.app.Activity and android.widget.TextView;

Here is the final code of the MainActivity.java file:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
TextView tv = new TextView(this);
tv.setText("Hello, World ! ");
setContentView(tv);
}
}

Leave a Reply