XML Tutorial XML stands for extensible Markup Language. XML is a markup language much like HTML used to describe data. XML tags are not predefined in XML. We should define our own Tags. Xml is well readable both by human and machine. Also, it is scalable and simple to develop. In Android we use xml for designing our layouts because xml is a lightweight language so it doesn t make our layout heavy. NOTE: This course is for basic concepts of XML in line with our existing Android Studio project. 1 First, let s look at the project structure created and maintained in Android studio for XML. The image above is known as the module directory, in Android studio our application is a module that is simply named app by default. The app directory shows manifest directory, java directory, res (which stands for resources) directory and there is a separate directory named Gradle Script. For this course we will be looking at only the XML related directories. Let s take a look at the XML files in these directories and their uses. Manifest Directory: this contains a single file called AndroidManifest.xml. It is used to define all the components of an application. It includes the names of our application packages, our Activities, services and the permissions that our application needs.
res directory: this contains a number of sub-directories, which are: 2 Drawable: this is where we put our graphics. It includes xml files that are used to provide various graphics to the elements or views of application. Layout: this is where we put xml files that define the appearance of our screen or it can also be describe as xml files are used to define the actual UI (User interface) of our application. It holds all the elements (views) or the tools that we want to use in our application. Menu: this where we put other xml files that defines menus. Mipmap: this contains a number of png files known as launcher icons.
3 Values: this contains a bunch of xml files that defines colors, dimension, strings and styles. Let s look deeper into the Android manifest file. The Android manifest file is an xml file that describes the app to the operating system and the application framework. Let s take a look at the
4 file. The file s root element is always named manifest, and there is a required name space declaration that is then used prefix is then used in all the attributes throughout the file. Within the manifest there is an application child element and it has a number of properties that controls the
appearance and behavior of the app for the user. 5 The allowbackup attribute turns on or off the back-up feature of the app. This allows certain data files to be uploaded to Google servers and then restored when an application is re-installed (e.g whatsapp messages and media restore). The icon attributes points to a graphic. This is something called a resource id. The prefix starting with the @ points to the mipmap folder in the res directory. Which is sometimes shown as the label attributes use a string resource. It starts with a resource prefix: @string, that points to file that are in the values directory and specifically the values that are defined in the string.xml file. this is defined in the style.xml file. Within the application, there is an activity that is defined. Each activity must be registered in the application manifest.
6 the name refers to the Java class that implements the activity. The dot(.) is a shorthand code that means the base package. the label refers to the string resource app_name. in the application. the theme here can be used to override whatever theme that was set this is used to control how this activity can be used and each application should have one activity that is marked as the launcher activity. Let s put some of this knowledge to practice. The exercise for this class is to change our application name and icon. How to change the application name, we change the application name using the label attribute. Notice that the label attribute appears twice: in the application element and in the activity element.
7 The activity label overrides the application label. Let s proceed to changing the app name. Hold down ctrl key and then click on the hello world in the label attribute of the activity element. It takes you to the string resource definition, in a file named strings.xml Or you can navigate this file by clicking on the res directory, then click on values folder and click on strings.xml file. Change the name from Hello World to MyApp To confirm the changes let s run our app. Click from the menu bar, it shows your virtual device.
Click Ok 8 This will run our virtual device (it will take a few minutes to launch, please be patient). When it finishes, we should see this
Also you can confirm this by clicking on the back arrow in our virtual device 9
It will take you to the home menu of your device. 10
Then you click on the menu, to see the application on our device. 11
This will show the apps on the device. 12
13 You will notice that our app has been renamed. The icon on the app is the default android icon for applications. Let s proceed to change the icon. Copy this image and paste it to your desktop
14 This will be the image we will use as our new app icon and we will navigate to it on our Desktop. Let s proceed. Go to your app menu Right click, choose new then image asset It takes you to this screen called the image asset studio. This is where we will change our icon.
Click on the icon circled in the image above to navigate to the image we want to use as our app icon. It shows the select path menu 15
Navigate to your desktop and select the image you pasted there. 16
Click OK and notice the icon has changed 17
18 Click next and Click Finish Our icon has been successfully changed. To confirm, click on the res directory, then click
19 mipmap Then double click on the any of the ic_launcher.png file to preview the launcher image Finally let s run our app to confirm the change on our device Click on the run icon It shows that our virtual device is connected, then click
OK Our app will re-launch 20
21 Click the back icon It shows the apps on the device and you will notice that the icon has changed.
Congratulations, you have successfully changed the app name and icon image, which brings us to the end of the XML class. Thank you 22