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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2blog.helloworldapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".HelloWorldActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Activity:

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
package com.java2blog.helloworldapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class HelloWorldActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_world);
    }
}

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(R.layout.activity_hello_world);
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_hello_world"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.java2blog.helloworldapp.HelloWorldActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>
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:
  • <resources>
        <string name="app_name">HelloWorldApp</string>
        <string name="welcome_java2blog">Welcome to Java2blog</string>
    </resources>
    
  • Make change in TextView attribute android:text as "@string/welcome_java2blog"
  • Bingo!! You have completed basic exercise.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012