Android id collision mechanism for R.java

We all know that when generating an id for Android using

@+id/foo

Android creates for us an entry in R.java like:

 public static final class id {
        public static final int foo=0x7f060005;
 }

What happens if there is a name collision in different xml files (let's say, inside two layouts)? The @+id mechanism ensures us to overwrite the id name if another one still exist, but which one is generated in R.java for us?


Asked by: Roland176 | Posted: 20-01-2022






Answer 1

The @+id/foo syntax will add if the id doesn't exist or use the existing id.
When you findViewById, it will operate on the view on which you call the method.

So, if you have nested Views, your id will be unique for each view. e.g. View1 -> View2 both have foo. View1.findViewById(R.id.foo) will be different from View2.findViewById(R.id.foo)

edit: I guess the main thing to mention is that two layouts can't have the same id. For more information on the id constraint: http://d.android.com/guide/topics/ui/declaring-layout.html

Answered by: Anna996 | Posted: 21-02-2022



Answer 2

I tried a simple Hello World application with the following xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="text1"
/>

<TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="text2"
/>

</LinearLayout>

Both my textviews have the same id. It compiles fine, runs fine, renders both the TextViews, and when i do a findViewByid() the first one gets found and all my function calls like setText are applied to it. Ideally, the AAPT should catch this but apparently it doesn't. It wont break something terribly unless the programmer relies on the id's. So its kind of like saying: if you are dumb enough to write such a code then you deserve to crash.

The AAPT won't care too much about it. For it, this is like a simple extension of Views with no explicit ids provided by the programmer.

Answered by: Freddie878 | Posted: 21-02-2022



Answer 3

I think it simply reuses the identifier if it's already generated. I tend to reuse IDs quite a bit, and have never run into a problem.

Answered by: Alberta675 | Posted: 21-02-2022



Similar questions

Android - Setting bitmap collision detection after rotation by matrix

How do I create code for collision detection on a bitmap rotated by a matrix? canvas.drawBitmap(mAnimation, mainMatrix, null); mainMatrix is derived from a PathMeasure so i do not know the rotation The bitmap is rotated around x=0,y=0 and thus a circle around the centre will not work (or at least be too big) thanks


Collision Detection In Android

Ok, so whats the best way to detect collisions in Android? The only examples I can find on it all involves finding when the two objects are directly over each other. Personally I was thinking about using bounding boxes, but even that I do not know how to do, and then I still desire something a little more precise (per-pixel?).


Collision detection with opengl es and android

I have been following the tutorials at http://blog.jayway.com/2010/01/01/opengl-es-tutorial-for-android-%E2%80%93-part-iii-%E2%80%93-transformations/ and I was wondering how I would go about implementing some 2d collision detection. I currently have 2 objects displayed on screen (1 of ...


java - Terrain and Collision in a Game

I am attempting to create a new Game in which a vehicle drives along the ground, but also has the capability to use thrusters to become temporarily airborne. I am relatively new to game programming, but I am rather good at coding in Java, C, C++ etc., so I do not think that should be an issue (thought the first iteration of the game is in Java). My problem is that I am starting from scratch, and can not think of a...


Collision detection of 2 lines in java (Android)

Very simple question. How do you achieve collision detection with lines drawn in Java? Just lines. No rectangles, circles or images, bitmaps.. just lines. By the way these lines are not straight. They are built of hundreds of very small lines that represent movement of a player (their gps coords as they move), so they meander all over the place as the player moves. All the lines are connected. The end point of one ...


android - Collision Detection and Collision Response

I've been trying my hand at representing a few moving/rotating objects using androids Opengl. Now I'd like to allow them to collide and respond realistically. Researching the issue, I can find many resources that advise me on ways to determine whether two 3d shapes are colliding in the current frame (or whether they'll be colliding in the next frame). However, I'm having trouble finding resources describing how to...


java - 2-D Collision crash course

Can anyone give me a reference to sites, blogs, articles, or post themselves a crash course or easy help guide in 2-D collision detection for Android? I looked at collision code from an online tutorial but they didn't explain the collision part of the code - just the part of implementing sprites. I am not using OpenGL, I am using Canvas for my game. I have searched around but I keep finding stuff for OpenGL or C++ but I am...


android 2d arc collision detection

i have a rotated arc drawn using android 2d graphics c.drawArc(new RectF(50, 50, 250, 250), 30, 270, true, paint); the arc will rotate while the game is running , i wanna know how i can detect if any other game objects(rects ,circles) collide with it ?? this is the first time for me to write a game :) i saw something like this in


Android collision detection

Description: I've got a project which is nearly finished now, but I've noticed the collision is not really working. It's a snake-like game that can be controlled via the touch-screen, so sharp (?, sorry german) angles are possible. At the moment I just leave a bit of tolerance (ignore the first 2 sprites) to enable a bit of turning. The main problem is that the sprites are being rotated which resul...


sprite collision Andengine (Android)

I develop a game in android. I able to find collision between two object. But i need to find collision only when two object collided(Not in blank space of image)In j2me ,i able to make that. Sample code: helicopterSprite.collidesWith(buildingSprite, true) But in Andengine , having only helicopterSprite.collidesWith(buildingSprite) Considered one image . Its c...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top