{"id":6,"date":"2021-10-07T04:57:57","date_gmt":"2021-10-07T04:57:57","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/workla\/?p=6"},"modified":"2021-10-11T17:59:22","modified_gmt":"2021-10-11T17:59:22","slug":"android-app-file-fundamentals","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/workla\/2021\/10\/07\/android-app-file-fundamentals\/","title":{"rendered":"Android App File Fundamentals"},"content":{"rendered":"\n<p>It can be quite overwhelming to start looking at code in a new android app you are creating.&nbsp; Even creating an empty app in Android Studio creates dozens of file for you, and yet that empty project actually only produces one screen with the words \u201cHello World\u201d written.&nbsp; For this post, I want to make you familiar with what files you should focus on as a developer just beginning with creating Android apps.<\/p>\n\n\n\n<div style=\"height:37px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We are going to talk about 4 main types of files in this post: activities, layout files, the manifest, and the build.gradle files.  I have created a sample empty app to show the simplest version of each of these files.<\/p>\n\n\n\n<div style=\"height:74px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<div style=\"height:74px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Let\u2019s start with the two easiest files to understand, the activities and layout files:<\/p>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">Activities:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"796\" height=\"424\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/mainactivity.jpg\" alt=\"\" class=\"wp-image-7\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/mainactivity.jpg 796w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/mainactivity-300x160.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/mainactivity-768x409.jpg 768w\" sizes=\"auto, (max-width: 796px) 100vw, 796px\" \/><\/figure>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>These are the parts of an app, something a user can do.\u00a0 So you might have, for example, a home screen activity and a picture capture activity.\u00a0 The home screen might make an API call to fill a list of items. \u00a0The picture capture might use the phone\u2019s built in camera to allow the user to take a picture.\u00a0 If you want your app to do anything (have any sort of action associated with it), that is all set up in your activity (As a note, there are more complicated items such as fragments that you could place in an activity and the fragment might contain logic, but for simplicity, when beginning with Android, activities will be the bread and butter files you use most).\u00a0 When creating our sample empty app, Android Studio automatically creates one starting activity for you: MainActivity.\u00a0 All MainActivity does in our basic app is tell the system to load a screen and which layout file to use.\u00a0 From there, you can, for example, set up click listeners for buttons, or send the user to another activity etc. In our example, this file is in Kotlin.<\/p>\n\n\n\n<div style=\"height:74px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">Layout Files:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/main-activity-layout-xml-1024x526.jpg\" alt=\"\" class=\"wp-image-8\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/main-activity-layout-xml-1024x526.jpg 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/main-activity-layout-xml-300x154.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/main-activity-layout-xml-768x394.jpg 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/main-activity-layout-xml.jpg 1321w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This tells the system what to initially show when an activity inflating this layout loads.\u00a0 The layout is an xml file containing one parent layout with child views contained within it.\u00a0 Views can be anything: TextViews, ImageViews, Buttons, CardViews, a nested layout, or maybe even a custom view you created.\u00a0 You arrange the views in the layout and set any initial settings for that view.\u00a0 For example, you can specify the size, location and image for the image view. You can set the text, text color, font etc for the text view.\u00a0 (Alternatively you don\u2019t have to specify these right away in the layout file. You can instead programmatically set them in the activity after, for example, info from an API call.)<\/p>\n\n\n\n<div style=\"height:74px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">Relationship between activities and layout files:<\/p>\n\n\n\n<p>An Activity won\u2019t show anything unless it inflates a layout, and a layout won\u2019t be seen until inflated by an activity.&nbsp; So typically in order any create a screen, you are going to have to create both of these files for each screen.<\/p>\n\n\n\n<div style=\"height:74px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">Where they live:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"661\" height=\"1024\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/folder-structure-661x1024.jpg\" alt=\"\" class=\"wp-image-9\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/folder-structure-661x1024.jpg 661w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/folder-structure-194x300.jpg 194w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/folder-structure-768x1189.jpg 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/folder-structure.jpg 868w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/figure>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I have chosen to show you the file structure using the \u201cProject\u201d tab in Android Studio, as this will mimic how the files appear when uploaded to Github (If instead you are viewing the \u201cAndroid\u201d tab in the file display, you will see Android reorganizes and hides some files to focus on the most commonly used items).<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>So inside of our SampleApp app, we have a bunch of folders, but the folder we want to focus on is the app module folder.&nbsp; An app can have multiple modules in it, and each module will show up as a folder under the project [1].&nbsp; You might have an app with multiple modules when you want to split up features or you have code for different devices like an android phone vs android watch, but for our simple project we are only going to have one module, the app module.<\/p>\n\n\n\n<div style=\"height:37px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Looking in the app module, we see under src\/main that we have a java folder and a res folder.&nbsp; The java folder will contain all of your Java and Kotlin files, so all of your activities will live in here.&nbsp; As you can see, we have MainActivity within com.example.sampleapp.&nbsp; The other folder in main is the resources folder, or res.&nbsp; This contains anything that your activities might use.&nbsp; So, this will be where our activity_main.xml layout file lives.&nbsp; It and all other layouts will be the in the layouts folder within the resources folder.&nbsp; Of interesting note, the resources folder also contains a values folder.&nbsp; Within that folder we have files for strings.xml, colors.xml etc.&nbsp; Instead of hard coding strings or colors into your activity or layout files, define them there and then you can use the variables anywhere within the module without fear of typos [2].<\/p>\n\n\n\n<div style=\"height:74px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<div style=\"height:74px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Now let\u2019s look at a couple of helper files that make the app run &#8211; the android manifest and the build.gradle files:<\/p>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">AndroidManifest:&nbsp; <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1016\" height=\"793\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest.jpg\" alt=\"\" class=\"wp-image-10\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest.jpg 1016w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest-300x234.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest-768x599.jpg 768w\" sizes=\"auto, (max-width: 1016px) 100vw, 1016px\" \/><\/figure>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can see the AndroidManifest file within src\/main.&nbsp; This file defines a lot about the app itself.&nbsp; It lists the requirements of the device the app is to be installed on and the permissions the app needs when running (like access to your contacts or your camera etc) [3].&nbsp; It also contains a list of the important components within your app.&nbsp; It acts kind of like a table of contents, so the system knows what is inside the app without having to scan the entire APK.&nbsp; If your activity is not listed within the manifest, your system will have no idea it exists and it will never be used [3].&nbsp; This makes the manifest an important file to understand because when you add a new activity to your starter app, you must remember to ensure it also appears in the manifest.<\/p>\n\n\n\n<div style=\"height:85px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\">Build.gradle files: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1008\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/gradle-1024x1008.jpg\" alt=\"\" class=\"wp-image-11\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/gradle-1024x1008.jpg 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/gradle-300x295.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/gradle-768x756.jpg 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/gradle.jpg 1247w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Gradle is responsible for taking your files and building them into the APK that can then be installed on a device.&nbsp; The build.gradle files allow you specify anything about the build that is necessary for your app to run.&nbsp; Even while first learning about Android development, there is one key thing you may need to add to the build.gradle file to make your app work: dependencies.&nbsp; These dependencies might include, for example, a library that helps with api calls, or a library that provides robust testing tools.&nbsp; The build.gradle file is where you define which dependency and what version you need.&nbsp; You may notice that there are two build.gradle files showing, one in the SampleApp project and one in the app module.&nbsp; Project build.gradle files allow you to define things that should be applied to all modules.&nbsp; Module build.gradle files allow you to specify different builds for different modules within the same app [4].<\/p>\n\n\n\n<div style=\"height:86px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<div style=\"height:90px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>A working knowledge of these four file types gives you a solid foundation for starting Android app development.\u00a0 Hopefully this short peek into the key files within your app will make the jump into Android app development feel less daunting.<\/p>\n\n\n\n<div style=\"height:90px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Sources:<\/p>\n\n\n\n<p>[1] \u201c<em>Project Overview<\/em>,\u201d  Aug 2, 2021 .&nbsp;Accessed&nbsp;on: Oct 6, 2021.&nbsp;[Online]. Available: <a href=\"https:\/\/developer.android.com\/studio\/projects\">https:\/\/developer.android.com\/studio\/projects<\/a><\/p>\n\n\n\n<p>[2] \u201c<em>App Resources Overview<\/em>,\u201d  Jul 14, 2021 .&nbsp;Accessed&nbsp;on: Oct 6, 2021.&nbsp;[Online]. Available: <a href=\"https:\/\/developer.android.com\/studio\/projects\">https:\/\/developer.android.com\/guide\/topics\/resources\/providing-resources<\/a><\/p>\n\n\n\n<p>[3] \u201c<em>App Manifest Overview<\/em>,\u201d  Oct 4, 2021 .&nbsp;Accessed&nbsp;on: Oct 6, 2021.&nbsp;[Online]. Available:  https:\/\/developer.android.com\/guide\/topics\/manifest\/manifest-intro<\/p>\n\n\n\n<p>[4] \u201c<em>Configure Your Build<\/em>,\u201d  Oct 4, 2021 .&nbsp;Accessed&nbsp;on: Oct 6, 2021.&nbsp;[Online]. Available: https:\/\/developer.android.com\/studio\/build<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It can be quite overwhelming to start looking at code in a new android app you are creating.&nbsp; Even creating an empty app in Android Studio creates dozens of file for you, and yet that empty project actually only produces one screen with the words \u201cHello World\u201d written.&nbsp; For this post, I want to make&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.oregonstate.edu\/workla\/2021\/10\/07\/android-app-file-fundamentals\/\">Continue reading <span class=\"screen-reader-text\">Android App File Fundamentals<\/span><\/a><\/p>\n","protected":false},"author":11613,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/users\/11613"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":9,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":23,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts\/6\/revisions\/23"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}