Lecture 4
Lecture 4
(Lecture-4)
• Introduction to UI Views
Working with
• RadioButton
• RadioGroup
• EditText
• TextView
2
Introduction to UI Views
• UI Views are the building blocks of an Android application's user
interface.
• They are responsible for drawing and event handling.
• Examples: Buttons, TextViews, ImageViews, RadioButtons, etc.
• Android UI components are generally derived from the View class.
3
Key Components of Android UI
6
What is an EditText?
• EditText is an input widget that allows users to enter and
modify text.
• It’s commonly used in forms, login screens, and comments
sections.
• Syntax: <EditText android:id="@+id/edit_text" />Code
• Example: Basic XML code for an EditText.Image: Example of
an EditText field in a login form.
7
Handling Input with EditText
• Capturing Input from EditText
• You can retrieve user input via getText() method
in Java or Kotlin.
• Example: val userInput = editText.text.toString()
• Code Example: Simple Kotlin code snippet for
retrieving and using EditText input.
8
9
What is a TextView?
• A TextView is a widget used to display static or dynamic text.
• It is often used for labels, instructions, or displaying results.
• Syntax: <TextView android:id="@+id/text_view" />
• Code Example: Basic XML code for a TextView.
• You can change the text of a TextView at runtime using
setText().
• Example: textView.setText("New text value")
10
What is LinearLayout?
• LinearLayout is a ViewGroup that aligns all its child views in a single direction,
either vertically or horizontally.
• It is one of the most basic and commonly used layouts in Android.
• Why Use LinearLayout?
• Simplicity:
• It is easy to implement and is useful for simple UI designs.
• Alignment:
• Ensures a clear, ordered arrangement of views.
• Flexibility:
• Can nest other layouts, allowing complex UIs.
• Weight Distribution:
• Efficient in distributing space among child views using layout weights.
11
Attributes of LinearLayout
android:orientation
Defines the direction of the child views.
vertical: Aligns child views vertically (stacked on top of each other).
horizontal: Aligns child views horizontally (side by side).
android:gravity
Aligns the child views inside the LinearLayout (e.g., center, left, right, etc.).
android:layout_weight
Specifies how much space a child view should occupy relative to others.
12
ListView
• is a ViewGroup that displays a vertically scrollable list of items, which can
be populated from an array, database, or other data sources.
• It is one of the most commonly used UI components for displaying large
sets of data efficiently in a mobile app
• Scrolling: Items in a ListView are displayed in a scrollable list format,
which makes it suitable for displaying long lists of items.
• Item Layouts: You can define custom layouts for each item in the list. The
list adapts to the number of items dynamically.
• Recycling Mechanism: ListView uses an internal recycling mechanism to
reuse the views that are out of sight, which helps in optimizing memory
and performance.
13
Basic Usage of ListView
• Step 1: Define the ListView in XML
• Step 2: Prepare the Data
• Step 3: Set Up the Adapter To bind data to the ListView, you need an
adapter (usually an ArrayAdapter or a CustomAdapter for more complex
layouts). An adapter converts the data into views for the ListView
• ArrayAdapter: This is one of the simplest types of adapters that takes data
from a source (e.g., an array or a list) and uses it to populate views in the
ListView.
14
Benefits of ListView
• Step 4: Handling Item Clicks You can handle clicks on individual items
using an OnItemClickListener.
• Efficient Handling of Large Datasets: Thanks to its recycling mechanism,
ListView is efficient in handling large amounts of data without consuming
too much memory.
• Versatility: ListView can be populated from different data sources,
including arrays, lists, and databases.
• Flexibility: You can fully customize the appearance and behavior of the list
items by providing custom layouts and adapters.
15
Limitations of ListView
• Performance: While ListView is efficient, for highly dynamic or complex
datasets.
• RecyclerView (a more advanced component) is often recommended as it
provides more flexibility and better performance.
• RecyclerView is a more modern and flexible replacement for ListView. It
supports more complex layouts, animations, and can handle large
datasets more efficiently.
• If you are working with static lists, ListView is simpler to implement. For
dynamic and performance-intensive lists, you may want to consider using
RecyclerView.
16
Thank You…!