1

I have some layout file which Lint is giving me the following warning about:

UselessParent

Summary: Checks whether a parent layout can be removed.

Priority: 2 / 10 Severity: Warning Category: Performance

A layout with children that has no siblings, is not a scrollview or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.

The part I'm confused about is in bold. My layout (before fixing this warning) is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/default_margin"
        android:paddingTop="@dimen/default_margin_small"
        android:paddingBottom="@dimen/default_margin_small">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingRight="@dimen/default_margin"
            android:orientation="vertical">

            <!-- TEXTVIEWS & EDITTEXTS HERE -->

        </LinearLayout>

    </ScrollView>

</LinearLayout>

But I fixed the warning like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/default_margin"
    android:paddingTop="@dimen/default_margin_small"
    android:paddingBottom="@dimen/default_margin_small">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingRight="@dimen/default_margin"
        android:orientation="vertical">

        <!-- TEXTVIEWS & EDITTEXTS HERE -->

    </LinearLayout>

</ScrollView>

This makes sense to me but the bold part above in the Lint check is confusing me a bit, because I do have a ScrollView inside a LinearLayout. Should I not be fixing it like this or is there no problem having a ScrollView as root element for a layout file since a ScrollView inherits from FrameLayout anyway?

rfgamaral
  • 16,546
  • 57
  • 163
  • 275

1 Answers1

2

There is no problem having a ScrollView as root element for a layout file. The lint complaint presumably was about your original root LinearLayout, which was not adding any value with just the ScrollView as a child.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I assumed as much but the "is not a scrollview" was confusing me. The whole sentence is a bit confusing (to me at least, English is not my first language) and I thought it was talking about the `ScrollView` inside the `LinearLayout`. Glad I could clarify that. Thanks. – rfgamaral May 02 '12 at 22:56
  • @RicardoAmaral: "I assumed as much but the "is not a scrollview" was confusing me." -- it's a generic message. AFAIK, they aren't templated based upon the specific view hierarchy. – CommonsWare May 02 '12 at 22:58
  • Yes, but it looked like it was giving some possible examples where one of those just happened to fit my situation. I guess I was mistaken :) – rfgamaral May 02 '12 at 23:02