Unit-3 Mad
Unit-3 Mad
Unit-3 Mad
1. Widgets:
2. Layouts:
Unit -3 1
Definition: Menus and toolbars provide options and actions that users
can access through buttons or overflow menus typically located at the
top or bottom of the screen.
1. Text Elements
TextView
Can be styled with different fonts, sizes, colors, and other properties.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="20sp"
android:textColor="#000000" />
EditText
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
Unit -3 2
android:hint="Enter text"
android:inputType="text" />
2. Buttons
Button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="onButtonClick" />
ImageButton
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:contentDescription="Icon Button" />
FloatingActionButton
<com.google.android.material.floatingactionbutton.Floa
tingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Unit -3 3
android:src="@drawable/ic_add"
android:contentDescription="Add Button" />
3. Input Controls
CheckBox
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the terms and conditions"
/>
RadioButton
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>
Switch
Unit -3 4
Typically used for enabling or disabling a feature.
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable feature" />
Spinner
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
SeekBar
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content" />
4. Pickers
DatePicker
<DatePicker
android:layout_width="wrap_content"
Unit -3 5
android:layout_height="wrap_content" />
TimePicker
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
5. Image Elements
ImageView
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground" />
6. Progress Indicators
ProgressBar
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge" />
SwipeRefreshLayout
Unit -3 6
Provides a swipe-to-refresh gesture.
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayou
t>
7. Menus
PopupMenu
<menu xmlns:android="<http://schemas.android.com/apk/r
es/android>">
<item android:id="@+id/item1" android:title="Item
1" />
<item android:id="@+id/item2" android:title="Item
2" />
</menu>
ContextMenu
Unit -3 7
Can be registered for any view.
registerForContextMenu(view)
<menu xmlns:android="<http://schemas.android.com/apk/r
es/android>">
<item android:id="@+id/item1" android:title="Item
1" />
<item android:id="@+id/item2" android:title="Item
2" />
</menu>
OptionsMenu
<menu xmlns:android="<http://schemas.android.com/apk/r
es/android>">
<item android:id="@+id/item1" android:title="Item
1" />
<item android:id="@+id/item2" android:title="Item
2" />
</menu>
8. Dialogs
AlertDialog
Unit -3 8
Can be configured with positive, negative, and neutral buttons.
DatePickerDialog
TimePickerDialog
9. Containers
CardView
Unit -3 9
Displays content with rounded corners and shadows.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:cardCornerRadius
="8dp"
app:cardElevation="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a card view" />
</androidx.cardview.widget.CardView>
xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scrollable content" />
</LinearLayout>
</ScrollView>
Unit -3 10
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
kotlin
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = MyAdapter(myDataset)
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Item" />
</LinearLayout>
2. RelativeLayout
<RelativeLayout
android:layout_width="match_parent"
Unit -3 11
android:layout_height="match_parent">
<TextView
android:id="@+id/firstItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firstItem"
android:text="Second Item" />
</RelativeLayout>
3. ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/firstItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Item"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/secondItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Item"
app:layout_constraintTop_toBottomOf="@id/firstIte
m"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Unit -3 12
4. FrameLayout
<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" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Overlay Text" />
</FrameLayout>
2. Property Animations
Unit -3 13
Animate properties of objects over time.
3. View Animations
<alpha
xmlns:android="<http://schemas.android.com/apk/res/an
droid>"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000" />
4. Drawable Animations
Frame-by-frame animations.
<animation-list xmlns:android="<http://schemas.android.co
m/apk/res/android>" android:oneshot="false">
<item android:drawable="@drawable/frame1" android:dur
ation="50" />
<item android:drawable="@drawable/frame2" android:dur
ation="50" />
<item android:drawable="@drawable/frame3" android:dur
ation="50" />
</animation-list>
Unit -3 14
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animation_list" />
By using these elements, layouts, and animations, you can create rich and
interactive user interfaces in Android applications.
Multiple
Single child at a children
Single root with
time positioned Single row or
Hierarchy multiple
(overlapping relative to each column layout
constraints
views) other or the
parent
Complex, Moderate,
Simple for
handles all other requires nested Simple, easy to
Complexity single child
layouts' layouts for use
overlays
functionalities complex UIs
Good
High Better Good
performance,
performance performance performance,
Performance but nested
with few with flat but nesting can
layouts may
children hierarchy degrade it
impact
Simple linear
Overlapping
Complex and arrangements
views, simple Dynamic
responsive UIs (e.g., form
Use Case overlays (e.g., positioning of
with flat fields, vertical
progress bar child views
hierarchy or horizontal
over an image)
lists)
Unit -3 15
Layout File Small and Larger due to Larger with Small to
Size simple constraints nested layouts moderate size
Alignment
Overlapping Precise Alignment
based on
Alignment views without alignment using based on linear
relative
alignment constraints arrangement
positioning
Can be nested
Generally not Requires
but not
nested, used Often used to nesting for
Nestability recommended
for specific use reduce nesting complex
for complex
cases layouts
layouts
Highly
Used when
Less frequent, recommended
Usage in simple relative Used for simple
specific use due to flexibility
Modern Apps positioning forms and lists
cases and
suffices
performance
Animates Frame-by-
Animates any
properties of frame
property of an Animates layout
Basic Concept views animation of
object (not just changes
(translation, drawable
views)
rotation) resources
Dynamic UI
Complex Loading,
Simple UI updates
Usage animations and progress, and
effects (adding/removing
transitions visual effects
views)
Unit -3 16
Limited to
predefined Moderately
Flexibility Limited Highly flexible
drawable flexible
sequences
Performance
Performance
Less efficient More efficient depends on the
varies with the
Performance for complex for complex number of
complexity of
animations animations frames and
layout
size
Requires more
Easy to Requires
Easier to setup and
Ease of Use implement understanding of
implement understanding
using XML layout changes
of keyframes
<alpha xmlns:android="<http://schemas.android.com/apk/re
s/android>"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000" />
2. Property Animations
Unit -3 17
}
valueAnimator.duration = 1000
valueAnimator.start()
3. Drawable Animations
<animation-list xmlns:android="<http://schemas.android.co
m/apk/res/android>" android:oneshot="false">
<item android:drawable="@drawable/frame1" android:dur
ation="50" />
<item android:drawable="@drawable/frame2" android:dur
ation="50" />
<item android:drawable="@drawable/frame3" android:dur
ation="50" />
</animation-list>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animation_list" />
4. Layout Animations
<layoutTransition
xmlns:android="<http://schemas.android.com/apk/res/an
droid>"
android:animateParentHierarchy="false"
android:layoutAnimation="@anim/layout_animation" />
Unit -3 18