M4
M4
)
Views are instances of the View class, and all UI components in Android, such as buttons, text fields, and image views, are
subclasses of the View class. Views handle the display of content and interaction with the user, and they can be laid out in
various ways to build the interface.
Types of Views in Android:
1. TextView:
o A TextView is used to display text to the user. It can be used for showing simple text, styled text, or even
links.
o Common properties: setText(), setTextColor(), setTextSize(), setTypeface().
o Example: A label or a heading in the UI.
2. Button:
o A Button is a clickable UI element that the user can interact with. It is often used to trigger actions, such as
submitting a form or opening a new activity.
o Common properties: setText(), setOnClickListener().
o Example: A button to submit a form or navigate to another screen.
3. ImageView:
o An ImageView is used to display images. It can show images from resources, files, or even a URL.
o Common properties: setImageResource(), setImageURI(), setImageBitmap().
o Example: Displaying a logo or picture.
4. EditText:
o An EditText is a specialized subclass of TextView that allows users to input text. It's typically used for fields
like username, password, email, etc.
o Common properties: setHint(), setText(), setInputType(), setTextColor().
o Example: A text input field for a user to enter information.
5. CheckBox:
o A CheckBox allows the user to select or deselect an option. It is used when the user can choose one or
more options in a set.
o Common properties: setChecked(), setOnCheckedChangeListener().
o Example: A form with checkboxes for selecting preferences.
6. Spinner:
o A Spinner provides a drop-down list to allow users to select from a list of options. It is similar to a
dropdown menu.
o Common properties: setAdapter(), setOnItemSelectedListener().
o Example: A drop-down list for selecting a country or city.
Define fragment. Explain the life cycle of fragment.
A Fragment in Android is a modular section of an activity. It represents a part of the user interface or behavior in an
activity.
A fragment has its own layout, logic, and lifecycle, but it must be hosted within an activity. Fragments allow for more
flexible UI designs, especially for devices with larger screens, such as tablets, where you can display multiple fragments
at once.
A fragment is useful for:
• Reusing UI components and logic.
• Creating flexible layouts for different screen sizes.
• Managing complex UI behaviors by breaking down the activity into smaller reusable components.
o In Android, Dialog Boxes are used to display messages, ask for input, or perform actions without leaving the current
screen. There are different types of dialogs, such as AlertDialog, DatePickerDialog, and TimePickerDialog.
o AlertDialog: The most commonly used dialog for displaying alerts or prompts. You can create custom layouts or
include buttons for user responses (e.g., OK, Cancel).
Explain how to check internet connection in Android programmatically.
Steps to Check Internet Connection Programmatically
1. Check Network Availability:
o Use the ConnectivityManager class to get information about the device’s network connectivity.
o Check if there is a network connection available (either Wi-Fi or mobile data).
2. Check Network Type:
o If a connection is available, you can also check whether it is Wi-Fi or mobile data, or if it has internet
access (e.g., via HTTP request).
Allow Permission.
How multimedia can be implemented in Android? Explain
1. Playing Video in Android:
To play video in an Android app, you typically use the VideoView component or MediaPlayer.
• Using VideoView: VideoView simplifies video playback, allowing you to load and play video from local or online
sources.
Example: