Android Activity – 1
The primary and important term in android development is ‘Activity’. Android Layout will be backed by Android Activity. Activity is nothing but a JAVA class file which helps in supporting the screen layout in performing all actions. Basically, its all about showcasing inbuilt features of android. Hence, Activity class helps in inheriting the features of android and helps in defining the actions of the layout.
As we created the login screen in the previous tutorial, we shall define the supporting activity class file. All the Activity class files will be under ‘java’ folder inside that projects respective package. To create a new one, right click on the ‘java’ folder..
java -> New -> Java Class
After selecting ‘Java Class’, there will be a window prompting to select the package inside the ‘java’ folder. Two packages are present inside ‘java’ folder, one which is actual package for placing our Activity class files which support layouts and other is for class files that supports testing.
Select main/java folder inside the ‘Choose Destination Directory’.
Provide the name for the class file and remember that Java class file should contain the first letter as capital letter. After clicking ‘OK’, Java class file will be created inside the ‘java’ folder. let us have a look at the code of the class file.
This looks as any other normal Java class file. This class helps android layouts only if it inherits the features of android inbuilt classes. One such inbuilt class is “Activity”. Activity class is inherited by this class to make it an Android Activity. As we know the inheritance functionality of java, here we use it to inherit the Activity class as show below.
The Activity class is from “android.app” package of Android. Now this class is not just any other java class, it is referred as an Android Activity.
As we created the supporting Activity file for the login layout, both should be linked to make it a complete android activity and a meaningful screen layout. Activity class file has couple of override methods to be declared which will decide the life cycle of the Android Activity. One of such override method onCreate.
Right click inside the class of Activity. Select the ‘Override Methods…’
Select onCreate method from the List of Override methods.
The code of Activity class will be updated with the override method. When this Activity gets created then at the same moment the layout file should be linked to it. That enables Activity to show the layout file. Hence, the code that joins the Activity and layout file is –
As layout file is present inside the layout folder of resources, the same is referred as the parameter to the method setContentView. The code after importing the layout file to Activity will be as below.
Now our Activity is ready for further implementation of the defined functionalities.
Download this tutorial
334 total views, 1 views today