48

I wrote a simple layout which has an EditText, but it's showing the following warning message:

“No label views point to this text field”

While searching I found this and it solved that warning message, but did not get difference between both attributes android:id and android:labelFor. Any clarification?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
CoDe
  • 11,056
  • 14
  • 90
  • 197
  • 1
    [*this*](http://thecodeiscompiling.blogspot.com/2013/12/android-labelfor-waning-fix.html) might be helpful – Nir Alfasi Jul 14 '14 at 07:11
  • 1
    Possible duplicate of [Meaning of "No label views point to this text field" warning message](http://stackoverflow.com/questions/16896082/meaning-of-no-label-views-point-to-this-text-field-warning-message) – YEH Nov 29 '15 at 08:24

5 Answers5

58

android:id

Supply an identifier name for this view, to later retrieve it with View.findViewById() or Activity.findViewById(). This must be a resource reference; typically you set this using the @+ syntax to create a new ID resources. For example: android:id="@+id/my_id" which allows you to later retrieve the view with findViewById(R.id.my_id).

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol id.


android:labelFor

public static final int labelFor

Specifies the id of a view for which this view serves as a label for accessibility purposes. For example, a TextView before an EditText in the UI usually specifies what infomation is contained in the EditText. Hence, the TextView is a label for the EditText.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

Constant Value: 16843718 (0x010103c6)

UPDATE:

For example -

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical">
    <LinearLayout android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_width="match_parent">
     <TextView android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:labelFor="@+id/edit_item_name" 
    android:text="Item Name"/>
     <EditText android:id="@+id/edit_item_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Item Name"/>
    </LinearLayout>
  </LinearLayout>

Reference: android:id and android:labelFor.

sjain
  • 23,126
  • 28
  • 107
  • 185
  • 1
    in my case it is giving warning for EditText Field and when I update labelFor attribute in EditText, warning went. Here you update same attribute for TextView...I am still not clear in what reference it should be used. – CoDe Jul 14 '14 at 09:06
  • 14
    Whats the advantage of setting labelfor? – Psypher Dec 02 '14 at 19:04
  • 5
    @Psypher Accessibility purposes—without that, a screen reader has trouble knowing what's the label for an input field, and the blind can't "see" the label is next to a field. – Blaisorblade Sep 20 '16 at 18:55
  • 2
    It'd be good to explain the reason for the multiple `+`s, from reading the docs it sounds like they shouldn't be there. See comments below: http://stackoverflow.com/questions/24731137/difference-between-androidid-and-androidlabelfor#comment65480318_32918913 – Blaisorblade Sep 20 '16 at 18:56
  • For those who may not have the reputation to view deleted answers, I found this commend from a deleted answer very helpful: If the XML items get re-ordered, leaving out the "+" will fail. (And Android Studio likes to reorder the order when it doesn't matter). Adding it multiple times does no harm, and leaving it out is a formula for future headaches. – BlueMonkMN Dec 19 '17 at 01:03
  • The layout_width of the TextView and the EditText would be better as wrap_content. – Mike Poole Apr 06 '18 at 10:24
3

android:id defines the ID of this view.

android:labelFor references the ID of another view.

Henry
  • 42,982
  • 7
  • 68
  • 84
1

android:id Simply the view ID to (programmatically) access the view.

android:labelFor pops up sometimes in combination with the lint warning:

Missing accessibility label: provide either a view with an 'android:labelFor' that references this view or provide an 'android:hint'

So, for example: If you have an EditText that does not describe itself, Android wonders if you might have some title or text (TextView) that might describes what the EditText is used for. So referencing the id of the TextView basically gives you an idea what to put into the EditText with its text in the accessibility mode.

See: https://developer.android.com/guide/topics/ui/accessibility/principles#editable

Javatar
  • 2,518
  • 1
  • 31
  • 43
0

in addition to all answers if u don't use xml files for apps this is a brief explanation what for serves VIEW ID :

(btw in my opinion using xml sucks - my only xml file is manifest :D generated by gradle)

@IdRes - annotation for resource id

/** define resource id for view */
@IdRes 
int TEXT_VIEW_ID  = "111111";

/** create edit tex in code */
EditText myTextView = new EditText(Context);
/** set view id */
myTextView.setID(TEXT_VIEW_ID);
/** set layout params etc then attach or inflate as u wish to view hierarchy */    

/** use view id to find view */
EditText etFound = (EditText) View.findViewById(TEXT_VIEW_ID);

ps. ID is mandatory to preserve state of hierarchy view when Activity.onSaveInstanceState(Bundle) is used - so if u create in code (VIEW / WIDGET / LAYOUT etc.) don't forget to set it.

ceph3us
  • 7,326
  • 3
  • 36
  • 43
0

android:labelFor

Use this attribute to indicate that a View should act as a content label for another View.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/email_subject_label"
        android:labelFor="@id/email_subject" />
    <EditText
        android:id="@+id/email_subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

For more details: Android Accessibility - How to provide content labels

Benny
  • 2,233
  • 1
  • 22
  • 27