Mada Unit-1
Mada Unit-1
- Define the user interface elements (e.g., buttons, text views) using XML.
- Override the onCreate() method to inflate the layout and initialize UI elements.
Example code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Initialize UI elements
<activity android:name=".MyActivity"></activity>
Key Concepts:
View as a Rectangle:
Every View occupies a rectangular area on the screen, even if the content doesn't fill the entire space.
Superclass:
The android.view.View class is the base class for all UI components in Android.
Widgets:
Views are often referred to as "widgets" because they represent the visible and interactive elements of the UI.
Layout Parameters:
Views have layout parameters (like LayoutParams) that define their desired size and position within their parent container.
Measuring and Drawing:
Views go through a "measure" pass to determine their size based on layout parameters and content, and then a "draw" pass to render themselves
on the screen.
Event Handling:
Views are responsible for handling user input events like clicks, touches, and key presses.
Using Views:
In Android, a View is a user interface component that represents a rectangular area of the screen. Views
are the basic building blocks of an Android application's user interface.
1. Text View
2. Edit Text
3. Button
4. Image View
Displays an image.
5. List View
6. Grid View
7. Recycler View
8. CheckBox
A checkbox that can be checked or unchecked.
9. Radio Button
10. Spinner
11. ProgressBar
12. SeekBar
Each View has its own set of attributes and methods that can be used to customize its appearance and
behavior.
Layouts in Android SDK
Layout Managers (or simply layouts) are said to be extensions of the ViewGroup class.
They are used to set the position of child Views within the UI we are building. We can nest the
layouts, and therefore we can create arbitrarily complex UIs using a combination of layouts.
There is a number of layout classes in the Android SDK. They can be used, modified or
can create your own to make the UI for your Views, Fragments and Activities. You can display
your contents effectively by using the right combination of layouts.
Types of Layouts
1. Constraint Layout
Constraint Layout is a flexible layout that allows views to be positioned using
constraints instead of nesting layouts. It helps in reducing the view hierarchy and improves
performance by a lot. It is the most popular and recommended layout for designing complex
interfaces in modern Android applications.
2. Linear Layout
A Linear Layout aligns each of the view in a single direction either a vertical or
a horizontal manner. A layout in vertical manner has a single column of views, whereas in a
layout in horizontal manner, has a single row of views. It supports a weight attribute for each
view that can control the relative size of each view within the available space. However,
excessive nesting of Linear Layouts can lead to performance issues, so it’s best for
straightforward layouts with a few elements.
3. Frame Layout
Frame Layout is a simple layout mainly used to hold a single child view, though multiple views
can be stacked on top of each other. It is commonly used for overlays, fragments, and simple UI
components like image views.
4. Relative Layout
Relative layout allows views to be relative to one another or to the parent layout. This
makes positioning flexible as we can position views to the left, right, top, or bottom of other
views. However after Constraint Layout was introduced, the use of Relative Layout has ceased
since Constraint Layout is much better as handling large UIs.
5. Table Layout
Table Layout
arranges views in a
grid-like format using
rows and columns,
similar to an HTML
table. It is useful for
displaying structured
data, such as forms or
spreadsheets. Each
row consists of multiple TableRow elements, and views within rows can be assigned specific
column spans.
6. Grid Layout
Introduced in Android 4.0 (API level 14), the GridLayout is an advanced and more
flexible alternative to TableLayout. It divides the screen into a matrix of rows and columns,
allowing precise placement of elements without needing of nested layouts. It is efficient for
creating grid-based UIs like image galleries or dashboards.
7. Coordinator Layout
Coordinator Layout is an advance layout that provides motion and interactive capabilities
between views. It is mostly used for implementing components like the Collapsing Toolbars, and
swipe-to-dismiss behavior. It allows views to work based on user actions, such as scrolling,
swipe gestures and animations.