Hay friend lets dig step by step Android Code execution with simple Hello World App code:
Before going to dig a code first lets understand some UI parts in android as bellow:
User
Interface (UI) Group:
UI group is used to specify how to arrange
the UI components.
Following are the UI groups used in android:
1)
Linear Layout :-Component arranged in One after another manner.
2)
Table Layout :-Components arranged in row and columns manner.
3)
Frame Layout
4)
Relative Layout- By using this layout it is possible to move,resize and place UI components wherever on the screen.
5)
Grid Layout
UI
Components:
1)
Text View
2)
Edit Text
3)
Button [Custom, Image, Toggle, Radio, Switch, Checkbox
etc.]
4)
Image View
5)
Autocomplete Text View
6)
Spinner
7)
List View
8)
Gallery
9)
Grid View
10)
Web view
11)
Fragment
12)
View Switcher or View Flipper.
At this point of time just remember these things detailed explanation about all UI groups and Components we will discuss in upcoming posts....
So lets begin with our Hello World code Execution.......
Fig 1: Create new Project give name "Hello_World"
Fig 2: This will be your Project Structure.
Steps to create an Activity file:
(**These steps are applied to any new activity
we create)
1) Create class with sub-type of
”android.app.Activity”
2) Same like main method in c/c++ /Java in android activity OnCreate() will invoke first
so provide the implementation for OnCreate( )
3) Design the UI in XML, by using the
following method we can set XML file context (UI) to the java file as shown bellow....
“SetContentView (R.layout.xml-file)”
Example of: “Hello World” app:
res->layout->activity_main.xml
<?xml
version="1.0" encoding="utf-8"?> '
Here you can see UTF-8 as default it is nothing but a Unicode format use in it
In Android Studio default you will get as Relative Layout change it to Linear Layout, Here xmlns means "XML namespace " specifies package
where as namespace is nothing but the collection of Classes ,Interfaces,and collections etc.
< LinearLayout xmlns: android=http://schemas.android.com/apk/res/android
android:layout_width="match_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
And we can have orientation of components we are placing in Layout as Horizontal or Vertical as required
Main_activity.java
file
package com.example.ygutte.Hello_World; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends Activity {
//must extends the Activity class @Override protected void onCreate (Bundle savedInstanceState) {
//Bundle is a class used to get state of an activity
super.onCreate (savedInstanceState);
SetContentView (R.layout.activity_main);
// to maintain the status of activity of supper class e.g. Function Overloading
/* Here we set the layout of screen as we designed in above xml file so that we use
SetContentView () –converts xml to java
-As abstract between xml and java file */
}
}
Hope you understood the code now you can start yours......
Happy Coding...........