UI Componenets and Layout (8 Marks)
UI Componenets and Layout (8 Marks)
Control Flow
Control means managing how an app behaves.
1
Flow means the direction of something from one point to another.
Control flow refers to the order in which the instructions or code are
executed based on conditions, events, and user interactions.
Control Flow:
1. Open the Android Studio.
2. Select File -> New -> New Project.
3. Select Empty Activity and click ‘Next’.
4. Give the Project Name and click ‘Finish’.
1. Activity:
- An activity is a class that represents a single screen. It is like a Frame
in AWT.
- They dictate the UI and handle the user interaction to the smart phone
screen.
- Example: Login screen, Home screen.
- An activity is implemented as a subclass of Activity class:
public class MainActivity extends Activity {
2. Services:
- A service is a component that runs in the background without UI to
perform long-running operations.
- They handle background processing associated with an application.
- For example: A service might play music in the background while the
user is in a different application, or it might fetch data over the
network without blocking user interaction with an activity.
- A service is implemented as a subclass of Service class:
public class MyService extends Service {
3. Broadcast Receivers:
- Broadcast receivers is a simply respond to broadcast messages from
other applications or from the system.
- They handle communication between Android OS and application.
- Example: Battery low alert, SMS received notification.
- A Broadcast Receiver is implemented as a subclass of
BroadcastReceiver class and each message as an Intent object.
public class MyReceiver extends BroadcastReceiver {
public void onReceive(content, intent) {
3
}
}
4. Content Providers:
- A content provider component supplies data from one application to
others on request / Content Providers are used to share data between
the applications.
- They handle data and database management issues.
- Example: Contacts app sharing data with WhatsApp.
- A content provider is implemented as a subclass of ContentProvider
class:
public class MyContentProvider extends ContentProvider {
public void onCreate() {
}
}
Other Components:
1. Fragments: Fragments are like parts of activity. An activity can display one or more fragments
on the screen at the same time.
2. Views: A view is the UI element such as button, label, text field etc. Anything that you see is a
view.
3. Layouts
4. Intents:
Intent is used to invoke components. It is mainly used to:
4
o Launch an activity
o Display a web page
o Display a list of contacts
o Broadcast a message
o Dial a phone call etc.
For example, you may write the following code to view the webpage.
5. Resources
6. Manifest
It contains informations about activities, content providers, permissions etc. It is like the web.xml file in
Java EE.