Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between android:id and android:labelFor?

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?

like image 434
CoDe Avatar asked Jul 14 '14 07:07

CoDe


People also ask

What is id in android studio?

android:id. Resource ID. A unique resource name for the element, which you can use to obtain a reference to the ViewGroup from your application.

What is android ID XML?

For example, when you specify an ID to an XML resource using the plus sign — in the format android:id="@+id/myView" —it means that a new ID resource with the name “myView” needs to be created, and if it does not exist, create a unique integer for it and add to R. java .


3 Answers

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.

like image 186
My God Avatar answered Oct 06 '22 14:10

My God


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().

like image 34
Jitender Dev Avatar answered Oct 06 '22 15:10

Jitender Dev


android:id defines the ID of this view.

android:labelFor references the ID of another view.

like image 3
Henry Avatar answered Oct 06 '22 15:10

Henry