unit-3
unit-3
• Control Flow:
- In control flow we studied that how we can execute the practical
- How we can create applications and what steps are required for creating application.
5. New Project is created with Given Name the ActivityMain.xml and MainActivity.java these 2 files
are generated
• Android Application are made up of different components that work together to create an
application. These components are called as building blocks of application.
1. Activities:
- An activity is like a screen in your app. For example, the login
- It’s the part of the app that the user interacts with. It displays the
2. Services:
- A service is a component that runs in the background to do
3. Broadcast Receivers:
- A broadcast receiver listens for messages or events from the
• 4. Content Provider:
- A content provider manages app data and allows it to be shared with other apps.
- It helps in accessing, sharing, and storing data like contacts, photos, or app databases.
- e.g. The Contacts app provides access to saved contact details for
1. Manifests Folder:
- This folder will contain a manifest file (AndroidManifest.xml) for our android application.
- This manifest file will contain information about our application such
as android version, access permissions, metadata, etc. of our application and its components.
- The manifest file will act as an intermediate between android OS and our application.
2. Java Folder:
- This folder will contain all the java source code (java) files which we'll create during the
application development.
- Whenever we create any new project/application, by default the class MainActivity.java will
create automatically.
3. res (Resources) Folder:
- It's an important folder that will contain all non-code resources, such as bitmap images,
UI strings, XML layouts
- It's a best practice to add all the images in a drawable folder other than app/launcher icons
for the application development.
- It will contain different density type of icons such as hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi, to
use different icons based on the size of the device.
4) Gradle Folder:
- Gradle folder is used to manages the build process of your project or application & dependencies.
- Gradle folder include the tool that builds your app. It contain configuration files.
- Gradle manages building process which are – Compiling your code, managing external libraries,
creating APK files for different Android version or devices.
• Android UI Components:
Main components used in Android UI Design. These are like building blocks that help developers
create screens and interactions in apps:
1. Text Components
- TextView: Displays text (e.g., titles, labels).
- EditText: Input field where users can type text (e.g., for login forms).
2. Button Components
- Button: A clickable button for actions (e.g., submit, next).
3. Layout Containers
- LinearLayout: Arranges items in a straight line (vertically or horizontally).
5. Image Components
- ImageView: Displays images in the app (e.g., profile picture, icons).
6. Input Components
- CheckBox: Lets users select one or more options.
7. Progress Indicators
- ProgressBar: Shows loading or progress (e.g., download progress).
8. Navigation Components
- Toolbar: A top bar with the app title, menu, or navigation buttons.
- Navigation Drawer: A sliding menu from the side for app navigation.
• Linear Layout
- LinearLayout in Android Studio is a layout that organizes UI elements (like buttons, text,
or images) in a straight line, either vertically (top to bottom) or horizontally (left to right).
Key Points:
1. Direction :- You can set the direction using the orientation attribute:
2. Equal Space :- You can control the size and spacing of each item by using weight.
between items.
3. Simple and Clean :- It’s perfect for creating straightforward layouts, like a list of buttons or
labels in a row or column.
Example:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
In this example:
• Absolute Layout
• An AbsoluteLayout in Android Studio is a type of layout that lets you place UI elements (like
buttons, text, or images) at specific x and y positions on the screen, measured in pixels.
Key Points:
1. Position-Based Layout:- You define the exact location of each element by specifying
2. Fixed Design:- The layout does not adjust automatically for different screen sizes or
3. Outdated:- AbsoluteLayout is not commonly used anymore because it is not flexible for
Example:
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_x="100dp"
android:layout_y="200dp" />
</AbsoluteLayout>
In this example:
• The button will appear at the (100dp, 200dp) position on the screen.
• It’s not responsive, meaning it doesn’t work well with devices of different screen sizes or
orientations.
• It is mostly avoided in modern app development. Use it only for very simple or temporary
designs where exact positioning is required and screen size doesn't matter.
• Frame Layout
A FrameLayout in Android Studio is a simple layout that is used to stack items on top of each other.
It places all its child views (like buttons, text, or images) in the top-left corner of the screen by
default, but you can move or overlap them by adjusting their properties.
Key Points:
1. Single Frame:- Think of it like a container or frame that holds one or multiple views
stacked together.
3. Simple Structure:- It’s easy to use when you don’t need a complex layout structure.
Example:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:layout_gravity="center" />
</FrameLayout>
In this example:
• The TextView is added second, and it appears on top of the image, centered using
layout_gravity.
• When you need simple overlapping views you can use Frame layout.
• For items that don’t require complex alignment, like showing text on an image or displaying a
loading spinner over a background, that time you use frame layout.
• Not ideal for complex layouts because it doesn’t allow flexible positioning like other layouts
(e.g., ConstraintLayout).
Table Layout
• A TableLayout in Android Studio is a type of layout that organizes UI elements (like text,
buttons, or images) into rows and columns, just like a table.
Key Points:
• Rows and Columns:- A TableLayout is divided into rows, and each row can have one or more
columns.
• Rows are created using TableRow, and each TableRow holds the elements for that row.
• Equal Row Height:- Rows can adjust their height automatically to fit the content.
• Flexible Columns:- Columns automatically size themselves based on their content, unless
specified otherwise.
Example:
Here’s how to create a simple table with two rows and three columns:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TableRow>
TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TableRow>
</TableLayout>
In this example:
• When you need to display data in a structured, table-like format that time you use Table
Layout.
• For designs where rows and columns make sense, like forms or schedules you use Table
Layout.
• May not handle dynamic content (e.g., many rows) as efficiently as other layouts like
RecyclerView.
• Relative Layout
- A RelativeLayout in Android Studio is a layout that arranges UI elements (like buttons,
text, or images) relative to each other or to the parent container. It allows you to create
more flexible and dynamic designs compared to simpler layouts like LinearLayout.
Key Points:
1. Positioning Relative to Parent:- You can align elements relative to the parent container, like
aligning them to the top, bottom, left, or right.
2. Positioning Relative to Other Elements:- You can place one element relative to another, such
as putting a button below or to the right of a text field.
3. Flexible Layout:- It adjusts well to different screen sizes and orientations, making it better for
responsive design.
Example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_below="@id/label"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In this example
• The TextView is positioned at the top center of the screen (aligned to the parent).
• The Button is placed below the TextView, and it’s also centered horizontally.
• It’s Flexible and allows precise positioning.
• For complex layouts with many elements, it can become difficult to manage.
• When you need to position elements dynamically relative to each other or the parent.