Bart Lizak
Bart Lizak

Reputation: 85

How to add ID to programmed layout item (android) in JAVA

I am adding multiple elements to layout in Java code. For example java coded before imageview to known layout with some layoutparams.

layout.addView(imageview, layoutparams);

I asume that I can't do it in XML because I don't know how many elements will be added (determined by user).

I need to set and know IDs to those added elements so I can refer to them later on.

What I need is something like (before adding):

imageview.setId(??);

Is this a good method to use? If so how do I use it? My main consern is that I am not sure how R.id works exactly. Can I for example make those IDs like 100001, 100002, 100003... ? Won't I override any? I will store those IDs in an list/array for use later in code.

public static int generateViewId ()

Added in API level 17 Generate a value suitable for use in setId(int). This value will not collide with ID values generated at build time by aapt for R.id.

What if it will colide during runtime because of multiple methods adding elements. Are there any sql-like transactions? BTW: Going for API17 isn't exactly what I want. Slightly over 50% of the market.

Upvotes: 3

Views: 1122

Answers (3)

Kiril Aleksandrov
Kiril Aleksandrov

Reputation: 2591

It is needed the ID to be unique for the findViewById method to work properly. However there is no guarantee that the ID is unique in the context of a ViewGroup. If you use the xml way to generate a layout, the android build tools generate unique IDs for all elements but if you create and add them manually to the container, the ID is not generated. If there are multiple views with equal IDs, the findViewById method will return the first view from the tree view structure with ID equal to the given one.

You can use safely the generateViewId() method. There is no need to synchronize its usage (transactions as you call them) because the android guys have already done this for you. You can use it safely without worrying for duplicate ids.

I hope this will help you understand the concept and the need of the IDs :)

Upvotes: 1

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

AFAIK, imageview.setId(); won't harm your resource identification through getting this source from R file, but in your case, I'd prefer use tags:

imageview.setTag(100001);

and to get it:

int id = Integer.parseInt(imageview.getTag().toString().trim());

And if you are already using the view tag somehow, you can give it another tag with a key like your app-name:

imageview.setTag(R.string.app_name, 100001);

and to get the id:

int id = Integer.parseInt(imageview.getTag(R.string.app_name).toString().trim());

Upvotes: 0

John P.
John P.

Reputation: 4388

If you need to get a reference to a certain View that is added dynamically to a bunch of other similar Views, you might want to use a loop similar to the one below:

for(int i = 0; i < parent.getChildCount(): i++) {

   View view = parent.getChildAt(i);

}

If you need more control than that then you may want to check out the View.setTag() method. Using this, you can create a unique tag for each View and retrieve it later on in a loop.

final ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setTag("I'm a tag!");

for(int i = 0; i < parent.getChildCount(): i++) {

   View view = parent.getChildAt(i);

   if(view != null && view.getTag() == "I'm a tag!") {

       // Do something with the View

   }

}

Although the code above should work, I might suggest approaching the issue from a different angle depending on what you intend to do with the Views.

Upvotes: 2

Related Questions