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!
- 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.
- Next, let's talk about layout resources:
- layout: This directory stores our layout files. Additionally, for specific models, we do screen adaptation, such as 480*320 phones, we create another set of layouts, like: layout-480x320.
- 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.
- Next, let's talk about the values directory:
demens.xml: Defines dimension resources.
string.xml: Defines string resources.
styles.xml: Defines style resources.
colors.xml: Defines color resources.
arrays.xml: Defines array resources.
attrs.xml: Used more when customizing controls, custom control attributes!
theme: Similar to styles but affects the entire application or specified Activity, usually changing the window appearance. Can be used in Java code via setTheme or in Androidmanifest.xml by adding the theme attribute to <application...>! PS: You might see values directories like: values-w820dp, values-v11, etc. The former w stands for tablet devices, 820dp for screen width; the latter v11 means it will be used after API (11), i.e., Android 3.0.
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.
Finally, there's animation. There are two types of animations: property animations and tween animations:
animator: Stores XML files for property animations.
anim: Stores XML files for 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.1 2015 Latest Android Basic Tutorial Table of Contents
-1.1 Background and System Architecture Analysis
1.8 Project-Related Analysis (Various Files, Resource Access)
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Field)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
3.6 Responding to System Setting Changes (Configuration Class)
4.4.2 Further Exploration of ContentProvider – Document Provider
5.2.1 Fragment Example: Implementing Bottom Navigation Bar (Method 1)
5.2.2 Fragment Example: Implementing Bottom Navigation Bar (Method 2)
5.2.3 Fragment Example: Implementing Bottom Navigation Bar (Method 3)
5.2.4 Fragment Example: Bottom Navigation Bar + ViewPager for Swiping Pages
5.2.5 Fragment Example: Simple Implementation of News (Shopping) App List Fragment
- 6.1 Data Storage and Access - File Storage Read and Write
- 6.2 Data Storage and Access - SharedPreferences for Saving User Preferences
- 6.3.1 Data Storage and Access - Introduction to SQLite Database
- 6.3.2 Data Storage and Access - Further Exploration of SQLite Database
- 7.1.1 Android Network Programming Essentials and HTTP Protocol Study
- 7.1.2 Android HTTP Request Headers and Response Headers Study
- 7.1.3 Android HTTP Request Method: HttpURLConnection
- 7.1.4 Android HTTP Request Method: HttpClient
- 7.2.1 Android XML Data Parsing
- 7.2.2 Android JSON Data Parsing
- 7.3.1 Android File Upload
- 7.3.2 Android File Download (1)
- 7.3.3 Android File Download (2)
- 7.4 Android WebService Call
- 7.5.1 WebView (Web View) Basic Usage
- 7.5.2 WebView and JavaScript Interaction Basics
- 7.5.3 WebView Considerations After Android 4.4
- 7.5.4 WebView File Download
- 7.5.5 WebView Cache Issues
- 7.5.6 WebView Handling Webpage Error Code Information
- 7.6.1 Socket Network Basics Preparation
- 7.6.2 TCP Protocol Based Socket Communication (1)
- 7.6.3 TCP Protocol Based Socket Communication (2)
- 7.6.4 UDP Protocol Based Socket Communication
- 8.1.1 13 Types of Drawable in Android Summary Part 1
- 8.1.2 13 Types of Drawable in Android Summary Part 2
- 8.1.3 13 Types of Drawable in Android Summary Part 3
- 8.2.1 Bitmap (Bitmap) Comprehensive Analysis Part 1
- 8.2.2 OOM Issues Caused by Bitmap
- 8.3.1 Detailed Explanation of Three Drawing Tool Classes
- 8.3.2 Drawing Class Practical Examples
- 8.3.3 Paint API - MaskFilter (Mask)
- 8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 1)
- 8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 2)
- 8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 3)
- 8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 4)
- 8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 5)
- 8.3.9 Paint API - ColorFilter (Color Filter) (1/3)
- 8.3.10 Paint API - ColorFilter (Color Filter) (2/3)
- 8.3.11 Paint API - ColorFilter (Color Filter) (3/3)
- 8.3.12 Paint API - PathEffect (Path Effect)
- 8.3.13 Paint API - Shader (Image Rendering)
- 8.3.14 Paint Enum/Constant Values and ShadowLayer Shadow Effect
- 8.3.15 Paint API - Typeface (Font Style)
- 8.3.16 Canvas API Detailed Explanation (Part 1)
- 8.3.17 Canvas API Detailed Explanation (Part 2) Clipping Methods Collection
- 8.3.18 Canvas API Detailed Explanation (Part 3) Matrix and drawBitmapMesh
- 8.4.1 Android Animation Collection - Frame Animation
- 8.4.2 Android Animation Collection - Tween Animation
- 8.4.3 Android Animation Collection - Property Animation - Introduction
- 8.4.4 Android Animation Collection - Property Animation - Further Exploration
- 9.1 Using SoundPool to Play Sound Effects (Duang~)
- 9.2 MediaPlayer for Audio and Video Playback
- 9.3 Using Camera to Take Photos
- 9.4 Using MediaRecord for Audio Recording
- 10.1 TelephonyManager (Telephony Manager)
- 10.2 SmsManager (SMS Manager)
- 10.3 AudioManager (Audio Manager)
- 10.4 Vibrator (Vibrator)
- 10.5 AlarmManager (Alarm Service)
- 10.6 PowerManager (Power Service)
- 10.7 WindowManager (Window Management Service)
- 10.8 LayoutInflater (Layout Service)
- 10.9 WallpaperManager (Wallpaper Manager)
- 10.10 Sensor Topics (1) - Introduction
- 10.11 Sensor Topics (2) - Orientation Sensor
- 10.12 Sensor Topics (3) - Accelerometer/Gyroscope Sensor
- 10.12 Sensor Topics (4) - Understanding Other Sensors
- 10.14 Android GPS Introduction
- 11.0 "2015 Latest Android Basic Tutorial" Completion Celebration
- 12.1 Android Practice: DrySister Viewing Girls App (Version 1) - Project Setup and Simple Implementation
- 12.2 DrySister Viewing Girls App (Version 1) - Parsing Backend Data
- 12.3 DrySister Viewing Girls App (Version 1) - Image Loading Optimization (Writing a Small Image Cache Framework)
- 12.4 DrySister Viewing Girls App (Version 1) - Adding Data Caching (Introducing SQLite)
12.5 DrySister View Girls App (Version 1) – 5. Code Review, Adjustment, and Logging Class Writing