Learning Android Game Development
上QQ阅读APP看书,第一时间看更新

Resource folder in detail

In the last chapter, we used the activity_fullscreen.xml file to edit the frontend of our application. Now, we will take a look at some more of these XML files and understand how they can be useful to us for making games. To understand these type of files, we must first know a little bit about them. To start off, the very basic information about XML is that it's a short form of Extensible Markup Language. Now, if you have studied HTML, you will know that its full form is quite similar to it--HyperText Markup Language. It's quite similar in syntax as well, but the function of an XML file is to hold data. If you go by the definition of an XML file, it goes something like this: XML is a software and hardware-independent tool for storing and transporting data.

You can read more about XML files at https://en.wikipedia.org/wiki/XML.
We have not yet seen an XML file in the code yet, so let's do that. Click on Text as shown in the following screenshot:

The Text mode for code editing is just besides the Design button

Now, you can actually see the XML code opened up:

This is your default XML code

Pay close attention to this code, and you will find something like this:

<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextView"
tools:text="helloWorld"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

If you see this, you can observe your Hello World! text on the very first line of this code. The data that we changed visually in our previous chapter can be changed through code over here. It is almost the same for all components, and you will learn about different components as you practice further.

Now, this is not the only type of XML file. As we read in the definition, XML files are used to store data. Let's see the other kinds of XML files, which can be used to store data. We can use these files for storing game scores, filenames, text data, and so much more. Let's take one such new type of XML file, which is already available in our project folder to understand further:

The strings.xml file contains all your string data and is in the res/values/ folder

Navigate to the app/res/values/ folder and double-click on the strings.xml file.

You can see the code for this file when you open it. Here, you can observe that there are multiple values and an ID to each value in the form of a name. Observe carefully the second line that reads as follows:

<string name="app_name">First Game</string>

Remember this name? We had set this as our app name when we started the project. It is stored in the value app_name. Also, if you go back and search for app_name using Ctrl + on your activity_fullscreen.xml file, you will find this entry. Go ahead and explore a little for yourself.

Also, check out the other files to get an understanding. Here's a basic explanation for the four XML files in our project folder:

  • attrs.xml: This declares custom theme attributes that allow changing the styles that are used for button bars, depending on the API level
  • colors.xml: This defines colors that can be used in hex code
  • strings.xml: This holds data for all string-related values
  • styles.xml: This sets the base theme of the application

So, that's it about XML files. Let's now move on to something even more interesting--inputs.