0

I am quite interested and confused both. Sometimes ago I was working on a project who has already implemented by some one. And after worked on that i found that he was using a unique number for find ID for its UI elements. Just like if we need to find id for TextView then we write,

 TextView textview = (TextView)findViewById(R.id.text);

but in that code it was like,

 TextView textview = (TextView)findViewById(012345678);

something like this.I have searched a lot in project also tried out to find that number but i couldn't. In R.java file i have also checked but there was also not. Its unique id its generated automatically as usual but i couldn't find a number in whole project.

So my question how can we use like this way? Is this possible??

TextView textview = (TextView)findViewById(012345678);

If this is possible then where we can define that id number and use it for UI elements.

Help will be appreciated!!

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • have you try to look for the hex ? – Blackbelt Sep 22 '14 at 09:17
  • Yes. But i couldn't figure out because i have checked in whole project but i couldn't found anything at all – Piyush Sep 22 '14 at 09:18
  • I guess he is just using the values taken out `R.java`. I evaluate this technique as **very poor** and definetely **not suggested**. It's much much better to use the classic way: by name – Phantômaxx Sep 22 '14 at 09:22
  • @FrankN.Stein No for that i have also checked R.java file and its id was different. – Piyush Sep 22 '14 at 09:23
  • Different like a hex to decimal conversion? – Phantômaxx Sep 22 '14 at 09:24
  • Yes it was totally different. – Piyush Sep 22 '14 at 09:26
  • 2
    So he's probably assigning the ids **manually** via `setId()` (awful!!). **The only reason for doing so is that the controls are created programmatically. + The only reason to create controls programmatically is that you have no other chance** – Phantômaxx Sep 22 '14 at 09:30
  • May be you are right..But for setId() i had searched careful there is not found anything. – Piyush Sep 22 '14 at 09:46
  • http://stackoverflow.com/a/13241629/3049917 – Top Cat Sep 22 '14 at 09:47
  • @TopCat Thanks for link. But please don't mind he had never created programmatic ally. I understood from this link but i am not satisfied 100% because of that used code. – Piyush Sep 22 '14 at 09:49
  • Just as a hint `012345678` is an octivial number ;-) – rekire Sep 22 '14 at 09:56
  • check this may be help http://stackoverflow.com/questions/8460680/how-can-i-assign-an-id-to-a-view-programmatically – Narendra Sep 22 '14 at 10:17

2 Answers2

0

enter image description here

from folder gen you can find R.java there is a class named id like in image.

for example

public static final int action_bar=0x7f06001c;

so instead of R.id.action_bar i can use 0x7f06001c.

Nooh
  • 1,548
  • 13
  • 21
  • 3
    Your answer is crap. That file will been overriden when you edit any ressources. There is also a warning at the top of the file: **AUTO-GENERATED FILE. DO NOT MODIFY. This class was automatically generated by the aapt tool from the resource data it found. It should not be modified by hand.** – rekire Sep 22 '14 at 09:59
  • 1
    i know that editing autogenerated file makes erorr. in my answer i didn't tell to edit the auto-generated file. i expined how to get integer id.you misunderstood me @rekire – Nooh Sep 22 '14 at 10:31
  • Well I'm not alone, that is possible indeed but not an good idea. – rekire Sep 22 '14 at 10:36
  • ya, its makes difficult to handle @rekire – Nooh Sep 22 '14 at 10:38
0

That is not directly possible as you might think. You can define your own custom ids which are not used in any xml files. I think that is what you want. To do this create a new xml files in res/values/ids.xml with this content:

<resources>
    <item name="your_custom_id" type="id"/>
</resources>

Than you can use it as R.id.your_custom_id I think that is what you want.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • So for type can i write 012345678 ?? – Piyush Sep 22 '14 at 09:58
  • i think this should be in res/values/ids.xml http://stackoverflow.com/a/3217786/3049917 – Top Cat Sep 22 '14 at 09:58
  • Well that wouldn't be wrong however the filename does not really care AFIK. In the end all value xml files are combined to one file, but you are right so I'll update my answer. @PG_Android no that is a hardcoded marker for the build tools. – rekire Sep 22 '14 at 10:00
  • @rekire I have tried but its giving a error in r.java file – Piyush Sep 22 '14 at 10:04