Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🎨 UI – Views & Layouts  ·  Lesson 16

ScrollView, CardView & Material Components

ScrollView and NestedScrollView

When content is taller than the screen, wrap it in a scroll container. Use NestedScrollView (AndroidX) — it works well with toolbars that collapse and with CoordinatorLayout.

XML
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <!-- exactly ONE child -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <!-- many views here: text, images, cards, buttons … -->
    </LinearLayout>
</androidx.core.widget.NestedScrollView>
NestedScrollViewscrolls verticallyLinearLayout (the one child)holds everythingTextViews, ImageViews, Cards, Buttonsyour content
One child rule

HorizontalScrollView

XML – scrolling chips
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none">

    <com.google.android.material.chip.ChipGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:singleLine="true">
        <!-- chips … -->
    </com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>

MaterialCardView

XML – course card
<com.google.android.material.card.MaterialCardView
    android:id="@+id/cardCourse"
    style="@style/Widget.Material3.CardView.Elevated"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:clickable="true"
    android:focusable="true"
    app:cardCornerRadius="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:contentDescription="@null"
            android:scaleType="centerCrop"
            android:src="@drawable/course_android" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="Android with Kotlin"
            android:textAppearance="?attr/textAppearanceTitleLarge" />
    </LinearLayout>
</com.google.android.material.card.MaterialCardView>
StyleLook
Widget.Material3.CardView.ElevatedShadow
Widget.Material3.CardView.FilledTinted background, no shadow
Widget.Material3.CardView.OutlinedBorder

Material buttons

XML – button styles
<!-- filled (main action) -->
<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Enroll now" app:icon="@drawable/ic_add" />

<!-- tonal (secondary) -->
<com.google.android.material.button.MaterialButton
    style="@style/Widget.Material3.Button.TonalButton"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Add to wishlist" />

<!-- outlined -->
<com.google.android.material.button.MaterialButton
    style="@style/Widget.Material3.Button.OutlinedButton"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Syllabus" />

<!-- text button (least important) -->
<com.google.android.material.button.MaterialButton
    style="@style/Widget.Material3.Button.TextButton"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="Share" />
9:41▮▮ 85%Buttons⋮+ ENROLL NOWAdd to wishlistSyllabusShare
Filled → tonal → outlined → text (importance high to low)

Floating Action Button

XML + Kotlin
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:id="@+id/fabEnroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:text="Enroll"
    app:icon="@drawable/ic_add" />

// Kotlin – shrink to a round FAB when the user scrolls down
binding.scroll.setOnScrollChangeListener { _, _, y, _, oldY ->
    if (y > oldY) binding.fabEnroll.shrink() else binding.fabEnroll.extend()
}

Progress indicators

XML + Kotlin
<!-- spinner while loading -->
<com.google.android.material.progressindicator.CircularProgressIndicator
    android:id="@+id/loader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true" />

<!-- course progress bar -->
<com.google.android.material.progressindicator.LinearProgressIndicator
    android:id="@+id/progressCourse"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:trackCornerRadius="6dp"
    app:trackThickness="10dp" />

// Kotlin
binding.progressCourse.setProgressCompat(65, true)   // animate to 65%
binding.loader.isVisible = false

Example: course detail screen

9:41▮▮ 85%←Android Course⋮🖼️course bannerAndroid with Kotlin10 weeks · ₹5,999 · ⭐ 4.8Week 1: Kotlin basics5 lessonsWeek 2: Layouts & vi…6 lessons+ Enroll9:41▮▮ 85%←Android Course⋮Week 3: RecyclerView4 lessonsWeek 4: Navigation5 lessonsYour progress 65%Fee₹5,999ENROLL NOW
NestedScrollView with banner, cards, progress and buttons — scroll position 0 and further down

Common mistakes

  • Two children inside a ScrollView → crash "ScrollView can host only one direct child".
  • Child height match_parent inside ScrollView → use wrap_content (and fillViewport for short content).
  • Using the old android.widget.Button styles mixed with Material styles → inconsistent look; use MaterialButton everywhere (the theme does this automatically for <Button>).

Summary

  • NestedScrollView with exactly one child for long screens.
  • MaterialCardView for grouped content; Material button styles show importance.
  • FAB for the main action; Circular/Linear progress indicators for loading and progress.
✅ Practice

Build the course detail screen above: banner, title, 5 week cards, a progress bar at 40% and an extended FAB that shrinks when scrolling down.

Frequently Asked Questions

Why can a ScrollView have only one child?
ScrollView scrolls a single content view. Put a LinearLayout or ConstraintLayout inside it and place all your views in that layout.
Should I put a RecyclerView inside a ScrollView?
Avoid it — the RecyclerView loses its recycling and becomes slow. Use one RecyclerView with different view types, or a ConcatAdapter.
CardView or MaterialCardView?
Use MaterialCardView from the Material library. It supports Material 3 styles, strokes, checkable cards and theme colours.
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicToast, Snackbar & Dialogs Your progress: 16 / 43 (37%) · Lesson 17 / 43 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free