Well, this is the most necessary and initial part in android development. One should have knowledge about creating an Activity. It represents a single screen of your application, for more details check this Article on Activity and Activity Life Cycle
Every activity in android consist two files. One is JAVA file and another is XML fie. JAVA file helps you implement the functionality of components that you defined in your XML file. To create an layout you should have an XML file. This is the file where you create an user interface for your activity.
There are two approaches to create an Activity:
1. Create both the files(JAVA & XML) separately and define it in Manifest file
a) In android studio, Select File Tab and click New
b) Select Java Class Option in the list and give name to you Java file
Every activity in android consist two files. One is JAVA file and another is XML fie. JAVA file helps you implement the functionality of components that you defined in your XML file. To create an layout you should have an XML file. This is the file where you create an user interface for your activity.
There are two approaches to create an Activity:
1. Create both the files(JAVA & XML) separately and define it in Manifest file
a) In android studio, Select File Tab and click New
b) Select Java Class Option in the list and give name to you Java file
Enter any name and click OK.
c) To make it an Activity you need to extend Activity class in it.
public class FirstActivity extends Activity{
}
d) we need atleast one method to work with our Activity class. So, we will insert an override method onCreate() check the details in Activity Life Cycle
Press Cntrl + O in android studio and Select onCreate() from list and click OK.
Now Lets create an XML layout file for our Activity.
first few steps are same. Click File tab and select new then select XML and inside XML select Layout XML File.
Give a appropriate name to your layout file for example:first_layout.xml (Make sure there should not be any special character or Upper Case while declaring the name on underscore(_) is allowed.
now you have an layout for your activity.
add this line in you FirstActivity. java under its onCreate method below super.onCreate(savedInstanceState)
setContentView(R.layout.first_layout); // We are inserting User interface to our activity.
There is only one step left to make sure that our activity runs properly. You need to define this activity inside your manifest file.
Now you Activity is ready to run.
2) The another way far simple than the first one. you need not to create both files separately and you need not to bother about manifest changes as well.
Lets take a look on another way of creating an Activity.
Just select File Tab then select New and then Activity option. In Activity option, select Empty Activiy
It will show your an dialog box like screen where you need to enter your activity name.
Just Hit finish and you have an activity which is ready to work for you. This will automatically create Java file and XML file inside the Layout Folder of your project. And you will find your java file in src folder.
Thats all about Creating an Activity.
Thank you Author for Sharing Android Activity Tutorials.
ReplyDeleteVisit for Free Android Studio Tutorials