Incorrect Label Association

Id IncorrectLabelAssociation
Summary Labels reference unrelated or incorrect controls, misleading users
Severity N/A
Category Assistive Technologies / Labels and Associations
Affects Resource files and Kotlin and Java files
Implementation Manual testing - automatic detection of incorrect labelFor associations is challenging due to the need to understand semantic relationships between labels and form elements, which cannot be inferred from static analysis alone.

Symptom

Label elements have android:labelFor attribute pointing to wrong target element ID, causing incorrect programmatic associations between labels and controls.

Rationale

Creates confusion for assistive technology users by announcing incorrect label-control relationships, leading to misunderstanding of form structure, input expectations, and potentially causing users to enter data in wrong fields or misinterpret interface functionality.

Examples

XML Layout Example

<!-- Labels with wrong labelFor associations -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- Label pointing to wrong field -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Name:"
        android:labelFor="@id/email_field" />

    <EditText
        android:id="@+id/name_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter first name" />

    <!-- Email label pointing to name field -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email Address:"
        android:labelFor="@id/name_field" />

    <EditText
        android:id="@+id/email_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter email"
        android:inputType="textEmailAddress" />

</LinearLayout>

Kotlin Code Example

class IncorrectLabels : AppCompatActivity() {

    private fun setupIncorrectLabelAssociations() {
        val nameLabel = findViewById<TextView>(R.id.name_label)
        val emailLabel = findViewById<TextView>(R.id.email_label)
        val nameField = findViewById<EditText>(R.id.name_field)
        val emailField = findViewById<EditText>(R.id.email_field)

        // Wrong label associations
        nameLabel.labelFor = emailField.id  // Name label points to email field
        emailLabel.labelFor = nameField.id  // Email label points to name field
    }
}

GitHub issues under this tag