Activity is a very important component in android application development. It represents a screen which is used to interact with phone user. Commonly there are three important files for creating and use an activity: an XML layout file, a java class file, and AndroidManifest.xml.
- The XML layout file is used to design the app’s UI part. We can think of it as a View role in the MVC design pattern.
- The java file is used to manipulate layout files, listen and process user action, startup other activity to the user when needed. So we can think of it as a Control role in the MVC design pattern.
- AndroidManifest.xml: We config all activities in this XML file. You can also specify activity attributes such as set one activity as android app startup activity in this file.
1.How To Create One Activity In Android Studio.
- First, make sure you have selected the Project view’s Android subview.
- Then right-click the app folder in Android view, click the ” New —> Activity ” menu. There will list some activity template menu, you can select the one you need. In this example, we select the Empty Activity template.
- Then the Configure Activity window will popup. You can input or modify the activity name and layout name as you need. Commonly the layout name is separated by underlines. You can check the Launcher Activity checkbox to make it the android application startup activity.
- Click the Finish button to complete the activity creation process. After that, you can see the three core files in the Project View on the left panel.
- In this example, AndroidManifest.xml is located in the app/manifests folder. ButtonListenerExampleActivity java file is located in app / java folder. activity_button_listener_example.xml is located in the app/res/layout folder.
- Then you can double click the above file to write code in it. If you want to learn more detail about the above three files, you can read the article Android Hello World Example Project File Structure or Android Studio Button Listener Key Listener And Browse Url Example.