Easy Tutorial
❮ Decimal Decimals Are Converted To Binary Fractions Es6 String ❯

1.8 Project Related Analysis (Various Files, Resource Access)

Category Android Basic Tutorial

Introduction to This Section:

You might think the previous discussions seem unrelated to Android development, but that's only because you're looking at it now. You'll understand later when you look back! Alright, in this section, we'll use the Hello World project we created earlier as a starting point to understand the project structure and the two ways to access resources in Android. The IDE used in subsequent tutorials will be Android Studio, as Google has officially announced the termination of support for other IDE development environments by the end of the year!


1. Project Structure Analysis:

Most of our development time is spent on the following parts:


Next, we will explain the key parts:

-java: Where we write Java code, business functionalities are implemented here.

-res: A place to store various resource files, such as images, strings, animations, audio, and various forms of XML files.


1. Introduction to the res Resource Folder:

PS: Speaking of the res directory, it's also worth mentioning the assets directory, although it's not here, we can create it ourselves. The difference is that all resource files under the former will have corresponding resource IDs generated in the R.java file, while the latter will not; the former allows us to access the corresponding resources directly through the resource ID; the latter requires us to read through AssetManager in binary stream form! By the way, the R file can be understood as a dictionary, where each resource under res will generate a unique ID!

Next, let's talk about the relevant directories under the res resource directory:

PS: The mipmap directories mentioned below do not exist in Eclipse; in Eclipse, they are all drawable-prefixed. The difference is not significant, but using mipmap provides certain performance optimizations for image scaling. Different resolutions will select corresponding images from hdpi, mdpi, xmdpi, xxhdpi based on the screen resolution. Therefore, when you unzip someone else's apk, you can see that the same name images in the four folders have different sizes and pixels! Of course, this is not absolute. For example, if we put all the images in the drawable-hdpi folder, even if the phone should load images from the ldpi folder, if there are none in ldpi, it will still load images from hdpi! Additionally, there is another situation: if there are images in the hdpi and mdpi folders but none in ldpi, then the resources from mdpi will be loaded! The principle is to use the closest density level! Also, if you want to prevent Android from loading resources from different folders according to the screen density, you can simply add the android:anyDensity="false" field in the AndroidManifest.xml file!

  1. First, let's talk about image resources:

-drawable: Stores various bitmap files, (.png, .jpg, .9png, .gif, etc.) In addition, there may be some other types of XML files.

-mipmap-hdpi: High resolution, generally where we put our images.

-mipmap-mdpi: Medium resolution, rarely used unless the compatible phones are very old.

-mipmap-xhdpi: Ultra-high resolution, as phone screen materials improve, it is expected that more images will transition here.

-mipmap-xxhdpi: Ultra-ultra-high resolution, this is reflected in high-end phones.

  1. Next, let's talk about layout resources:
  1. Next, let's talk about menu resources:

-menu: Used more in the past when phones had physical menu buttons, now not so much. XML files for menu items can be written here. It's unclear if Google will introduce something new to replace menus.

  1. Next, let's talk about the values directory:
  1. Next, let's talk about the raw directory: Used to store various native resources (audio, video, some XML files, etc.). We can get the binary stream of resources through openRawResource(int id). It's similar to Assets, but resources here will generate a resource ID in the R file.

  2. Finally, there's animation. There are two types of animations: property animations and tween animations:


2. How to Use These Resources

Alright, knowing what resources there are, let's understand how to use them: As mentioned earlier, all our resource files will generate a resource ID in the R.java file, and we can access these resources through this resource ID. There are two scenarios for usage: in Java code and in XML code.

Using in Java Code:

Java Text:

txtName.setText(getResources().getText(R.string.name));

Image:

imgIcon.setBackgroundDrawableResource(R.drawable.icon);

Color:

txtName.setTextColor(getResouces().getColor(R.color.red));

Layout:

setContentView(R.layout.main);

Control:

txtName = (TextView)findViewById(R.id.txt_name);

Using in XML Code:

By using @xxx, you can get them, for example, here we get text and images:

<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background = "@drawable/img_back"/>

2. Deep Dive into Three Files:

Alright, next we'll dissect three important files in the project: MainActivity.java, layout file: activity_main, and Android configuration file: AndroidManifest.xmlPS: The image content may have some discrepancies, no time to make the diagrams, please understand~

MainActivity.java:

Code as follows

package jay.com.example.firstapp;

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);
    }
}

Code analysis:

Layout file: activity_main.xml:

Code as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

Code analysis:

We define a LinearLayout linear layout, define the architecture we need to use in the xml namespace, from ①

AndroidManifest.xml configuration file:

Code as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jay.com.example.firstapp" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Code analysis:

In addition to the above content:

① If the app includes other components, they must be declared in this file using type description syntax. Server:

② Permission declaration: Explicitly declare the permissions required by the program in this file to prevent the app from misusing services, inappropriately accessing resources, ultimately improving the robustness of the android app. android.permission.SEND_SMS, for example, indicates that the app needs to use the send message permission, which will prompt the user during installation. Relevant permissions can be found in the sdk reference manual!


Summary of This Section:

In this section, we have a detailed understanding of our Hello World project, what the relevant directories are for, what the resource files under res are, what their functions are, and how to use these resources! At the same time, we explain in detail the three most important files in the project through diagrammatic methods! By now, you should have a general understanding of the Android project! Thank you~

-1.0 Android Basic Tutorial

-1.0.1 2015 Latest Android Basic Tutorial Table of Contents

-1.1 Background and System Architecture Analysis

Follow on WeChat

❮ Decimal Decimals Are Converted To Binary Fractions Es6 String ❯