What are activities in android? How to create activities in android? How to create Hello World activity? How to create multiple activities in an android application? Example of activities in android application.
Activities in Android -
Activity is one of the basic building blocks otherwise known as application components in android system. An activity is basically a screen that user sees in your android application. An android app can have multiple activities (screens) to support multiple features e.g. suppose you’ve a gaming application so when the users launch your app they see one activity showing them multiple options like new game, continue, options, help or quit etc to choose from and each of these options are also activities. So when user selects say help option that activity is launched pushing your current running activity on to the back-stack.
Creating Activity in Android–
By default, when you create a new android project it is created as a single activity, a “Hello World” activity that prints hello world when the app is launched. You can modify it of course as you wish. Also you can add multiple activities according to your requirements. Three things are basically required for an activity to work correctly –
- A java file – This java file contains code or function that your activity performs. You need to place all your activity’s java files in the “src” folder inside your package.
- A layout file – This is an XML file that basically holds the UI (user interface) of your activity. Also you can put different UI elements like buttons, images etc or change the layout to linear, relative etc. All layout files reside in the res / layout folder in your project hierarchy. The default file inside this folder is the main.xml that is for your main activity.
- An entry in AndroidManifest.XML file – Manifest file is the heart of android system. It tells the android system all about your application including different activities, their intents etc. So each time you create a new activity you have to put an entry in this file to tell the android system that “hey this is another activity in my app”.