Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:28:56+00:00 2026-06-13T22:28:56+00:00

In an XML file, we can assign an ID to a view like android:id=@+id/something

  • 0

In an XML file, we can assign an ID to a view like android:id="@+id/something" and then call findViewById(), but when creating a view programmatically, how do I assign an ID?

I think setId() is not the same as default assignment. setId() is extra.

Can anybody correct me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:28:58+00:00Added an answer on June 13, 2026 at 10:28 pm

    Android id overview

    An Android id is an integer commonly used to identify views; this id can be assigned via XML (when possible) and via code (programmatically.) The id is most useful for getting references for XML-defined Views generated by an Inflater (such as by using setContentView.)

    Assign id via XML

    • Add an attribute of android:id="@+id/somename" to your view.
    • When your application is built, the android:id will be assigned a unique int for use in code.
    • Reference your android:id‘s int value in code using “R.id.somename” (effectively a constant.)
    • this int can change from build to build so never copy an id from gen/package.name/R.java, just use “R.id.somename”.
    • (Also, an id assigned to a Preference in XML is not used when the Preference generates its View.)

    Assign id via code (programmatically)

    • Manually set ids using someView.setId(int);
    • The int must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.)
    • For example, if creating and numbering several views representing items, you could use their item number.

    Uniqueness of ids

    • XML-assigned ids will be unique.
    • Code-assigned ids do not have to be unique
    • Code-assigned ids can (theoretically) conflict with XML-assigned ids.
    • These conflicting ids won’t matter if queried correctly (keep reading).

    When (and why) conflicting ids don’t matter

    • findViewById(int) will iterate depth-first recursively through the view hierarchy from the View you specify and return the first View it finds with a matching id.
    • As long as there are no code-assigned ids assigned before an XML-defined id in the hierarchy, findViewById(R.id.somename) will always return the XML-defined View so id‘d.

    Dynamically Creating Views and Assigning IDs

    • In layout XML, define an empty ViewGroup with id.
    • Such as a LinearLayout with android:id="@+id/placeholder".
    • Use code to populate the placeholder ViewGroup with Views.
    • If you need or want, assign any ids that are convenient to each view.
    • Query these child views using placeholder.findViewById(convenientInt);

    • API 17 introduced View.generateViewId() which allows you to generate a unique ID.

    If you choose to keep references to your views around, be sure to instantiate them with getApplicationContext() and be sure to set each reference to null in onDestroy. Apparently leaking the Activity (hanging onto it after is is destroyed) is wasteful.. 🙂

    Reserve an XML android:id for use in code

    API 17 introduced View.generateViewId() which generates a unique ID. (Thanks to take-chances-make-changes for pointing this out.)*

    If your ViewGroup 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:

    Here, values/ids.xml defines a custom id:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item name="reservedNamedId" type="id"/>
    </resources>
    

    Then once the ViewGroup or View has been created, you can attach the custom id

    myViewGroup.setId(R.id.reservedNamedId);
    

    Conflicting id example

    For clarity by way of obfuscating example, lets examine what happens when there is an id conflict behind the scenes.

    layout/mylayout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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:id="@+id/placeholder"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    </LinearLayout>
    

    To simulate a conflict, lets say our latest build assigned R.id.placeholder(@+id/placeholder) an int value of 12..

    Next, MyActivity.java defines some adds views programmatically (via 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<20; i++){
        TextView tv = new TextView(this.getApplicationContext());
        // One new TextView will also be assigned an id==12:
        tv.setId(i);
        placeholder.addView(tv);
    }
    

    So placeholder and one of our new TextViews both have an id of 12! But this isn’t really a problem if we query placeholder’s child views:

    // 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);
    

    *Not so bad

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created XML file,but I can't view it/output it.I know there is no
I can currently use sgml-pretty-print to pretty print an xml file in emacs, but
How can I define underlined text in an Android layout xml file?
I'm trying to assign the contents of an xml file to a var, like
I am using an XML file to store the values. This XML file can
How can I copy a remote xml file to my raw folder in order
How can I validate an XML file against a DTD that is stored locally
Hi I can't understand this error I want read this XML file: <?xml version=1.0
How can I save the content of an xml file to a database( in
I can see the Graphical Layout of the XML file within the /res/layout folder,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.