Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Android <code>id</code> overview</h2> <p>An Android <code>id</code> is an integer commonly used to identify views; this <code>id</code> can be assigned via XML (when possible) and via code (programmatically.) The <code>id</code> is most useful for getting references for XML-defined <code>View</code>s generated by an <code>Inflater</code> (such as by using <code>setContentView</code>.)</p> <h3>Assign <code>id</code> via <code>XML</code></h3> <ul> <li>Add an attribute of <code>android:id="@+id/</code>somename<code>"</code> to your view.</li> <li>When your application is built, the <code>android:id</code> will be assigned a <strong>unique</strong> <code>int</code> for use in code.</li> <li>Reference your <code>android:id</code>'s <code>int</code> value in code using "<code>R.id.</code>somename" (effectively a constant.)</li> <li><em>this <code>int</code> can change from build to build</em> so <strong>never copy an id</strong> from <code>gen/</code>package.name/<code>R.java</code>, just use "<code>R.id.</code>somename".</li> <li>(Also, an <code>id</code> assigned to a <code>Preference</code> in XML is not used when the <code>Preference</code> generates its <code>View</code>.)</li> </ul> <h3>Assign <code>id</code> via code (programmatically)</h3> <ul> <li>Manually set <code>id</code>s using <code>someView.setId(</code>int<code>);</code></li> <li>The <code>int</code> must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.)</li> <li><em>For example, if creating and numbering several views representing items, you could use their item number.</em></li> </ul> <h3>Uniqueness of <code>id</code>s</h3> <ul> <li><code>XML</code>-assigned <code>id</code>s will be unique.</li> <li>Code-assigned <code>id</code>s do <em>not</em> have to be unique</li> <li>Code-assigned <code>id</code>s can (theoretically) conflict with <code>XML</code>-assigned <code>id</code>s.</li> <li>These conflicting <code>id</code>s won't matter if queried correctly <em>(keep reading)</em>.</li> </ul> <h3>When (and why) conflicting <code>id</code>s don't matter</h3> <ul> <li><code>findViewById(int)</code> will iterate depth-first recursively through the view hierarchy <em>from the View you specify</em> and return the first <code>View</code> it finds with a matching <code>id</code>.</li> <li>As long as there are no code-assigned <code>id</code>s assigned before an XML-defined <code>id</code> in the hierarchy, <code>findViewById(R.id.somename)</code> will always return the XML-defined View so <code>id</code>'d.</li> </ul> <h3>Dynamically Creating Views and Assigning <code>ID</code>s</h3> <ul> <li>In layout XML, define an empty <code>ViewGroup</code> with <code>id</code>.</li> <li><em>Such as a <code>LinearLayout</code> with <code>android:id="@+id/placeholder"</code>.</em></li> <li>Use code to populate the placeholder <code>ViewGroup</code> with <code>View</code>s.</li> <li>If you need or want, assign any <code>id</code>s that are convenient to each view.</li> <li><p>Query these child views using placeholder.findViewById(convenientInt);</p></li> <li><p>API 17 introduced <code>View.generateViewId()</code> which allows you to generate a unique ID. </p></li> </ul> <p><strong>If you choose to keep references to your views around</strong>, be sure to instantiate them with <code>getApplicationContext()</code> and be sure to set each reference to null in <code>onDestroy</code>. Apparently <em>leaking</em> the <code>Activity</code> (hanging onto it after is is destroyed) is wasteful.. :)</p> <h3>Reserve an XML <code>android:id</code> for use in code</h3> <p><em>API 17 introduced</em> <code>View.generateViewId()</code> <em>which generates a unique ID.</em> (Thanks to take-chances-make-changes for pointing this out.)*</p> <p>If your <code>ViewGroup</code> cannot be defined via XML (or you don't want it to be) you can reserve the id via XML to ensure it remains unique:</p> <p><strong>Here, <em>values/ids.xml</em> defines a custom <code>id</code>:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;item name="reservedNamedId" type="id"/&gt; &lt;/resources&gt; </code></pre> <p><strong>Then once the ViewGroup or View has been created, you can attach the custom id</strong></p> <pre><code>myViewGroup.setId(R.id.reservedNamedId); </code></pre> <h2>Conflicting <code>id</code> example</h2> <p>For clarity by way of obfuscating example, lets examine what happens when there is an <code>id</code> conflict behind the scenes.</p> <p><strong>layout/mylayout.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;LinearLayout android:id="@+id/placeholder" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;/LinearLayout&gt; </code></pre> <p><em>To simulate a conflict, lets say our latest build assigned <code>R.id.placeholder</code>(<code>@+id/placeholder</code>) an <code>int</code> value of</em> <code>12</code>..</p> <p>Next, <strong>MyActivity.java</strong> defines some adds views programmatically (via code):</p> <pre><code>int placeholderId = R.id.placeholder; // placeholderId==12 // returns *placeholder* which has id==12: ViewGroup placeholder = (ViewGroup)this.findViewById(placeholderId); for (int i=0; i&lt;20; i++){ TextView tv = new TextView(this.getApplicationContext()); // One new TextView will also be assigned an id==12: tv.setId(i); placeholder.addView(tv); } </code></pre> <p>So <code>placeholder</code> and one of our new <code>TextView</code>s both have an <code>id</code> of 12! But this isn't really a problem if we query placeholder's child views:</p> <pre><code>// Will return a generated TextView: placeholder.findViewById(12); // Whereas this will return the ViewGroup *placeholder*; // as long as its R.id remains 12: Activity.this.findViewById(12); </code></pre> <p>*Not so bad</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload