How To Add Activity In Android Studio

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.

  1. 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.
  2. 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.
  3. 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.

  1. First, make sure you have selected the Project view’s Android subview.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

4 thoughts on “How To Add Activity In Android Studio”

  1. I am not familiar with Android Studio, when I add a new java class in Android Studio, I can not find the method public void onCreate in the new java class source file.

    And what I really want is to create an android activity such as the android app main_activity. I can find the super.onCreate(savedInstanceState) method in the main_activity, and it will connect to the layout XML file in this method.

    I want to make my new java class behave like the main_activity, how can I do that? Do I need to copy the public void onCreate method to the new java class. Can anyone give me some help? Thanks a lot.

  2. I have a problem, when i right click on a folder for example… Src/main/java/ to make an empty activity, i select new i should have empty activity and other options in list as you show but i have not in android studio.
    I can send you pics if I have your email
    Pls help me, tnx

    1. Please right click on the folder or project name in the andorid studio (app -> src – > main -> java -> package name (project name)), then click the Activity and select AcitvityType.Input the detailed information about the new android-activity, click the Finish button to complete.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.