Android #7 - Hello World example with Empty Activity | Breakdown |Part 2


Hello friends, welcome to the another tutorial of Programming Infinity. In this tutorial, we will know about the empty activity and also about the components, which as a whole makes the empty activity. It is generally known as the hello world example for the first time, when we create our project. It is called so, because it shows a text "hello world" in its UI, when we will create first project with taking activity as , empty activity in the activity dialog, which appears while creating our app project as described in the previous tutorial. It is known as the empty activity, because it doesn't contains any widgets in its layout, hence creating an empty space, where we can design our custom UI. We will start with this activity as our base for learning the UI designing with widgets and other components in the upcoming tutorials. So friends, let's begin to kNow about the empty activity with the help of hello world example...

Hello World Breakdown (Empty Activity)

Hello world is a simplest example app in android programming, which consists of an empty activity i.e. it doesn't contains any visible component. It only contains a layout, which is a specially designed space for storing widgets in different structures , varies from layout to layout. In hello world example, the activity only consists of a Textview, which has text "Hello World". Now let's know about the components of the hello world example.
Any app need resources to work properly, such as in the above example, a text resource is needed, which is taken as a value in Textview. These resources are of different-different types, such as colors, images , text etc. It also contains .java type classes as a resource , which is interpreted at runtime in android, when application is launched. It also contains dependencies declaration, which is used by the android OS to provide certain libraries access to app. And the most important part of the app is the interface, which provides some important function to the app as services, broadcast receivers etc. by communicating with the android OS. If any component used is not declared in this interface, it wouldn't be considered and hence, doesn't work.
These all are the resources which is used by the app to work properly. Now let's know about these in details considering the hello world example.

Components :-

1. MainActivity.java :-

It is a .java source code file which is executed by the Dalvik in the runtime, when certain .java class is called and performs the functions, declared in the code. In the hello world example, it is used to create layout and set it to the activity i.e. A layout with a Textview is first created and then it's view is set to a particular activity as defined in the code.

package com.example.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); 
}
}

The first line of code is the package name. It is always reuqired to write it whenever writing .java class code, otherwise it will show a error. In android studio, when you create a new .java class and previously generated class, already contains the package name as its first line of code. After this, there is code for importing library, which is required when the code is running in runtime to provide certain functions of library to app. When writing code with android studio, it automatically import the library to the code, hence making the work easier. Without the import of the library, error will occure and the app will not be compiled by the android studio or compiler. We had already learnt about the android Libraries, that which library is used for what function. In the hello world example, android.support and android.os type library is imported. The android.support library helps to create and display the UI, and the android.os library, is used for retrieving the data from android Operating system, that is the saved state of the app in the form of bundle.
Now we will know about the activity, which is a interface with which the user intercats. We will kNow more about the activity in the upcoming tutorials. In the hello world example, activity is used to draw view and set it to the activity. Here one method of activity is used that is a required method, when creating activity. Some more methods are there in the activity. We will know more about these in the activity tutorial. The onCreate() method is called, when there is need to draw view and set it to the activity as in the case of hello world example, a layout resource named activity_main is drawn and its view is set on the activity.

2. Manifest file :-

It is a auto-generated xml file which acts as a interface between android OS and app, by providing required resources , permissions to the app from android OS. It also contains information about the app, which is used by the android OS for various purposes.
e.g. :- It contains the app name, icon , version name etc. Which the android OS use for showing it in apps list with icon and app name.
It contains information about the app, the permissions required, declaration of application component used such as activity, service, broadcast receivers or content providers. Any fault in the declaration of these, will cause error in the app.

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

Above code is the default code generated by the manifest, when using an empty activity. The first line if code is the declaration of the xml, wHich tells the version of xml and the encoding which is used to compile xml code i.e. process the xml code. Second line of code is the root element of the manifest.xml file, which contains all the other required components.
After that some elements are used, so let's kNow about these elements in brief. Explained description will be avialable in the desired Components tutorial.
  • <application>....</application> :- It enclose all the information/ components related to application.
e.g :- The <activity>...</activity> component is a application component, and hence used in the application element.
 - Some important attribute :- 
1. icon :- It takes the path of the image/drawable as its value to set the icon of the app.
2. label :- It takes the text resource as its value, which is used as the app name.
3. supportsRtl :- It is a boolean attribute, which takes its value as true/false. If true is set as value of this attribute, then the app will support right to left layout, otherwise not. The Rtl means that the layout will become the mirror image of the previous layout.


4. theme :- It is a attribute, which is used to set the theme of the app.
5. allowBackup :- It is a boolean attribute, which is used to backup the resource of the android application. If sets to true, the backup will be allowed, otherwise not. We will kNow more about this in the upcoming tutorials.
  • <activity> :- It is a element, which is used to declare an activity. It takes attribute as name and label, which takes the class name and title for the activity respectively as its value.
In the hello world example, <intent-filter> element is used, which is used to provide some extra function to the activity. It contains action and category element in its body in the above code. Now let's kNow about these elements :- 
1. action :- It is used to declare an action for the component. In the above code, It takes name as its attribute with value "android.intent.action.MAIN", which declare that it is the entry point of the app i.e. if icon of the app is clicked, then the first activity that will open is this one.
2. category :- It is used to declare the category of the component. In the above code, It takes name attribute and it's value is "android.intent.caregory.Launcher" , which declares the activity in the category of launcher i.e. the app can be launched with the app icon.
If we forgot to write any one of these, then the app icon will not show in the apps or can't be launched from the icon click. So there is must to write both these to doesn't cause error to the app.
So friends, that's all for this tutorial. Hope u en it. In the next tutorial, we will know about, how to access resources in app, with the help of hello world example. If you have any problem regarding this topic, then ask me in comment box.
Thanks -
Programming Infinity (Samridh Sharma).

Comments