Difference between android:id and android:labelFor?

I write an simple layout which have and EditText, but it showing following warning message,

“No label views point to this text field”

  • Android - get selection of text from EditText
  • android.widget.LinearLayout cannot be cast to android.widget.TextView
  • Access file in JUnit test in Gradle environment
  • How to find file name of a file which is transferred via wifi direct mode in android?
  • AutoCompleteTextView detect when selected entry from list edited by user
  • Android open emailclient programmatically
  • While searching I got this and it solve that warning message as well. But did not got difference between both attribute android:id and android:labelFor. Any clarification !

    Related posts:

    how to find the distance between two geopoints?
    Constraint on performFiltering returns null on first character
    9-patch - 1px border is blurred
    android : ScrollView vs NestedScrollView
    Unit testing creating an SQLite database using Spock and Robospock
    How to force keyboard to show/hide?
  • Google webfonts not working using Web View in Android 4.0 and 4.2.2
  • Changing notification icon background on Lollipop
  • Pushwoosh leaking my activity
  • Viewpager background color bleeds through first page
  • Serializing Android Bundle for network and/or persistent storage?
  • Set an Alarm from My application
  • 4 Solutions collect form web for “Difference between android:id and android:labelFor?”

    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.

    The labelFor is an attribute for accessibility options. You assign this to a label so that if, on a form , user clicks a EditText field , android can know what to read (TalkBack for low vision users) to user.

    The id Supply an identifier name for this view, to later retrieve it with View.findViewById() or Activity.findViewById().

    android:id defines the ID of this view.

    android:labelFor references the ID of another view.

    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 😀 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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.