In the previous post we said 'Hello' to our first android app.This post will provide yo a good understanding about the basic structure of an android application.So get ready.Let us rock!
Basic Android project structure
Before you proceed further, you should be aware of a few directories and files in the Android project.Let us learn about the main directories in android studio project.They are manifests,java,res,drawable,layout,menu and so on.
1. Java
This contains the .java source files for your project. By default, it includes an MainActivity.java source file having an activity class that runs when your app is launched using the app icon.
2. res/drawable
This is a directory for drawable objects that are designed for low,medium and high-density screens.
3. res/layout
This is a directory for files that define your app's user interface.
4. res/values
This is a directory for other various XML files that contain a collection of resources, such as strings and colours definitions.
5. AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.
6. Build.gradle
This is an auto generated file which contains compileSdkVersion, buildToolsVersion, applicationId, minSdkVersion, targetSdkVersion, versionCode and versionName
MainActivity
The main activity code is a Java file MainActivity.java. Following is the default code generated for Hello World! application
package cooltechiz.blogspot.com.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Layout
The activity_main.xml is a layout file available in res/layout directory, that .This is basically your application's user interface. For your "Hello World!" application, this file will have following content related to default layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>Manifest
You must declare all permission,activities etc in a manifest.xml which resides at the root of the application project directory. so if you do not declare your component in this file, then it will not be considered by the OS. For example, a default manifest file will look like as following file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cooltechiz.blogspot.com.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So we have learnt about the basic structue of an android application and some of its important files.Hope you learnt basics of android project structure.Let us get into little bit advanced programming in next post,Cheers!
No comments:
Post a Comment