Android Project Structure in android Studio

In previous post, we have created hello world android application. In this post, I am going to explain structure of android application to understand it better.
When you created android application, your folder structure will look similar to below.

Lets understand above folder structure.

ApplicationManifest.xml:

ApplicationManifest.xml resides in src/main/.
Every application should have ApplicationManifest.xml in its root directory. It provides all important information to android system which system must have before it run the application.
We can declare following in ApplicationManifest.xml:

  • Package Name
  • Permissions of Application
  • Declaration of Activity , Services, broadcast receivers etc.
  • Minimum level of SDK and many more..

AndroidManifest.xml

All java classes resides in src/main/java folder.
Activity is java class which actually supports a screen on UI. It is like a frame in java.
Activity is predefine class in android and every screen must inherit to some activity to create a UI.
HelloWorldActivity.java

res:

res(src/main/res) is folder which represents resource folder in android. It has multiple sub folders.

drawable:

It is the folder in which you put images which you are going to use in your android app.

layout:

It is the folder in which you have layout xml file. Layout file represents design of the screen. Every activity class has layout file corresponds to it. If you notice in HelloWorldActivity.java , we have put following code.

setContentView actually binds layout to a activity. In our case, layout name is activity_hello_world.xml. If you open activity_hello_world.xml, you will see below screen.

You can drag and drop widgets to screen using design interface.
You can click on Text as highlight above and you will see below xml
activity_hello_world.xml

You can edit in above xml file also to make layout related changes.

mipmap :

This folder contains android application icon in different size.

values:

This folder will contains xml file which will have constant in it.
For example:
string.xml file have all strings declared in it, so you don’t have to hardcode anything in your java code.

Why you should not hardcode:
You have all strings in english as of now and you are going to release your application in french then if you hardcode everywhere then you need to change in search the strings and change in java code but if you do not hardcode anything, you just have to convert strings.xml strings in french and it will work.

Gradle scripts:

Gradle is open source automation tool similar to ant and maven. It is used to build project and include dependency in android studio. We will more about it later.

Exercise:

Lets do a small exercise to see if you understand above structure or not.
You need to make changes in hello world app and make output as below.

Do not hardcode “Welcome to Java2blog” in layout file. Put string contant in strings.xml

Solution:

    • Click on activity_hello_world.xml in project structure.
    • Drag “Hello world” string in Hello world app to center part using design layout.
    • Add string constant with “Welcome to Java2blog” in strings.xml as below:

  • Make change in TextView attribute android:text as “@string/welcome_java2blog”
  • Bingo!! You have completed basic exercise.

Add Comment