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.

Related Posts

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Instagram Music Search Not Working
    17 January

    Instagram Music Search Not Working – 8 Easy Fixes

    Table of ContentsReasons Behind Instagram Music Search Not Working.Fix Instagram Music Search Not Working.1. Update The Application.2. Switch Back To Personal Account.3. Clear Cache & Storage Data.4. Reinstall Instagram.5. Use Instagram Lite.6. Use A VPN Connection.7. Use DownDetector.8. Reach Out To Instagram Support Center.Frequently Asked Questions (FAQs):1. How do you add music to Instagram Stories?2. […]

  • Convert ASCII to char in C++
    15 January

    Convert ASCII to Char in C++

    Table of ContentsHow to Convert ASCII values to Char in C++?Convert ASCII to Char in C++ Using Char() FunctionConvert ASCII to Char in C++ Using C-Style CastConvert ASCII to Char in C++ Using the Assignment OperatorConvert ASCII to Character in C++ Using sprintf() FunctionConclusion The characters are represented using integers in most of the encoding […]

  • Split String by pipe in java
    15 January

    Split String by pipe(|) in java

    Table of ContentsHow to split String by pipe in javaUsing split() methodUsing StringTokenizerUsing Pattern class In this post, we will see how to split String by pipe in java. How to split String by pipe in java There are multiple ways to split String by pipe (|) in Java. Using split() method You can use […]

  • In this post, we will see how to generate random number between 1 to 10 in javascript. How to generate random number between 1 and 10 in javascript We can simply Math.random() method to generate random number between 1 and 10 in javascript. `Math.random()` returns a random number between 0(inclusive), and 1(exclusive). That means `Math.random()` returns always number lower than 1. We can use `Math.random()` with `Math.floor()` to generate random integer. Here is generic formula to generate random number in the range. Math.floor(Math.random() * (maximum - minimum + 1)) + minimum In our case, minimum = 1 maximum = 10 so it will be Math.floor(Math.random() * (10 - 1 + 1)) + 1 Math.floor(Math.random() * 10) + 1 So here is the program to generate random number between 1 and 10 in javascript. var randNum = Math.floor(Math.random() * 10) + 1; console.log(randNum) When you run above program, you will get below output: 3 You can obviously get differnt outout as we are generating random number here. Generate 10 random integers in range of 1 to 10 console.log("Generating 10 random integers in range of 1 to 10") for (let i = 0; i < 10; i++) { var randNum = Math.floor(Math.random() * 10) + 1; console.log(randNum) } Generate 10 random integers in range of 1 to 10 7 5 1 10 5 9 7 7 6 2 Generate random number in a range in javascript Here is generic formula to generate random number in a range. function generateRandomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } // Generate random number between 1 to 10 console.log(generateRandomInteger(1,10)) // Generate random number between 11 to 20 console.log(generateRandomInteger(11,20)) // Generate random number between 21 to 30 console.log(generateRandomInteger(21,30)) 4 17 28 In case, if you don't want to include maximum while generating random numbers, you can use below function. function generateRandomInteger(min, max) { return Math.floor(Math.random() * (max - min)) + min; } That's all about how to generate random number between 1 and 10 in javascript
    15 January

    Generate random number between 1 and 10 in javascript

    Table of ContentsHow to generate random number between 1 and 10 in javascriptGenerate 10 random integers in range of 1 to 10Generate random number in a range in javascript In this post, we will see how to generate random number between 1 to 10 in javascript. How to generate random number between 1 and 10 […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.