before going to start with actual coding let me tell you something about android activity
ACTIVITY :
activity is something that we see on androids screen i mean the screens that we see in android application like a login screen or a screen playing some audio or video any screen that we see in android is called activity a application may contain single screen or multiple screens i mean activities thus a activity contains two parts one is java part other is xml part .well xml part deals with the design means what we want to show to our users like buttons scroll activities tabviews webcomponents zoom in out buttons all comes under xml what we see is added in xml layouts these layouts will be in res-->layouts---> folder follwed my xml extension its petty easy to add things xml files
ACTIVITY :
activity is something that we see on androids screen i mean the screens that we see in android application like a login screen or a screen playing some audio or video any screen that we see in android is called activity a application may contain single screen or multiple screens i mean activities thus a activity contains two parts one is java part other is xml part .well xml part deals with the design means what we want to show to our users like buttons scroll activities tabviews webcomponents zoom in out buttons all comes under xml what we see is added in xml layouts these layouts will be in res-->layouts---> folder follwed my xml extension its petty easy to add things xml files
you see above image simple activity and left hand side you can see buttons from widgets that can be easily dragged and dropped in to activity and we can add anything we want from these widgets at the bottom of page you can see two tabs saying graphical layout and activity .xml layout the above picture shows graphical layout what we see in our applications there are plenty of options there all around if you switch back to xml layout you can see all the code you dont need to write any code for it graphical layout help you in adding what ever controls you need the code will be something like this which is automatically generated
<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"
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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
i havent written anything here its eclipse and adt plugins taking care of creating the code for us.before moving out further i want you look at the properties each widget or that we drag and drop have some properties assosiated with it that will help as in defining its shape and all how to see those properties graphically lets have a look
this is my properties tab its there in my eclipse environment at the right side if you dont see it in your eclipse right click on the widget and say showin--->properties it will pops out please note that every widget you add has its own properties some properties are initialized to default values there are somany properties available i can not explain them all you can learn one after the other but here i wanna say about one propertie called background you can see it in view in above image click it it will pop window asking you to choose file from drawble it will set background image for your widget you can set visibility you can specify the id which is unique name given to your widget you can specify visibility fonts style and all lets see i am going to add a button to our layout for it i am just going to drag a button to layout
see above two images i have dragged a button left side it shows properties of the button the green arrows you see those are here because we are using relative layout its something like its arranging button relative too top and left and in second image you can see the properties of button text the text that must appear on button you can change it like login signup sign in next previous and it too have background in view by using it we can set background of button
lets examine java part now that is we have used the button now what our button should do now i mean when button is pressed what our application should do it will be written using java for it we need to go to source folder and open .java extension files like mainactivity.java it wont have graphicall view we need to type everything in it lets see how this code look like
package far.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
above is the code for java file lets study the code one line after other pakcage far.test its name of the package its like packaged box with far.test it will internally creates files and folders in with far folder in it test folder name inside it will keep the java file in there next import these are the java classes that we use in our application like android.app.activity this is the java class that we will use in coding you dont need to bother about it eclipse will take care of it it will search and import required files next public class mainactivity extends activity there are several java terms here publich its acess speciefer saying mainactivity class is available whole of our application class is java keyword specifiying mainactivity which is name of activity is class type that extends acitivy that means mainacitivy is type of class which is an activity in java defination a class is something that wraps out methods methods are nothing but functions things that execute and do something thus activity has life cycle like activity goes in to stages i am naming some of them here
1. created
2. paused
3. resume
4.stopped
created sate is something when activity is first created like we have started app by clicking its icon of application we have methods in our app in mainactivity where we add code asking to do some work when activity is going throw these cycles the method below
protected void onCreate(Bundle savedInstanceState) {
created sate is something when activity is first created like we have started app by clicking its icon of application we have methods in our app in mainactivity where we add code asking to do some work when activity is going throw these cycles the method below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onCreate(Bundle savedInstanceState) this is the name of method where we code inside its body that is in between { } when what we want how our application should behave inside it well next line super.onCreate its calling android default code to be executed and passing parameter bundle saved instant state line followed by it it says setcontentview(r.layout.activity_main) we are asking to set the content of our activity to this following xml file R is the refrance file created by eclipse that holds refrences too all the widgets and layouts we create in our application its poiting to xml fine crated in layout folder and it will load that xml file in the view
ufffff i think this lesson gone too lengthy with lots of technical words dont worry try to digest it as it is without going more technical details and try to get learn some java you dont need java completely but you should know basics of any programming language and some basics of java they are not compulsory but they are recommeneded for speedy learning that is for today




No comments:
Post a Comment