How To Add Activity In Android Studio

Activity is a very important component in android application development. It represent a screen which is used to interact with phone user. Commonly there are three important files for create and use an activity: a 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 it as View role in MVC design pattern.
  2. The java file is used to manipulate layout file, listen and process user action, startup other activity to user when needed. So we can think it as Control role in MVC degisn pattern.
  3. AndroidManifest.xml : We config all activitys 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 sub view.
    android studio project view android sub view
  2. Then right click app folder in Android view, click ” New —> Activity ” menu. There will list some activity template menu, you can select the one you need. In this example, we select Empty activity.
    create new android activity
  3. Then the Configure Activity window will popup. You can input or modify activity name and layout name as you need. Commonly the layout name has under line in it to separate English words. You can check Launcher Activity checkbox to make it as the android application startup activity.
    create new activity settings dialog
  4. Click Finish button to complete the activity creation process. After that you can see the three core files in project view at left panel.
    add linear layout in android layout file
    In this example AndroidManifest.xml is located in app / manifests folder. ButtonListenerExampleActivity java file is located in app / java folder. activity_button_listener_example.xml is located in app / res / layout folder.
  5. Then you can double click above file to write code in it. If you want to learn more detail about above three files, you can read article Android Hello World Example Project File Structure or Android Studio Button Listener Key Listener And Browse Url Example.
(Visited 1,631 times, 9 visits today)
READ :   Android Nine Patch Image Example

Leave a Reply

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.