gilonm
gilonm

Reputation: 1859

R.id value requirements

I am currently working on an app, and I have noticed that if I assign any element (e.g. Textview) a numeric id value (such as android:id="@+id/1") - I get an error and it will not compile until I add a letter to the id.

My questions are:

1) Why are we not able to use numeric values?

2) Are there any other requirements of R id's?

Just trying to better understand the logic behind this..

I have tried searching with not much luck...

Thanks

Upvotes: 0

Views: 111

Answers (3)

Mark Buikema
Mark Buikema

Reputation: 2540

Variables can only start with letters or underscores. Other than that, there are no requirements.

Upvotes: 0

amirye
amirye

Reputation: 147

taken from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_"

i believe this goes the same for R.id's. meaning they have to start with a letter, "$" or "-".

for your second question , the convention for R.id is that it should be all lower case and no spaces between words just underscore "_".

Upvotes: 1

safaiyeh
safaiyeh

Reputation: 1715

To my understanding it is just Java convention. For example, you wouldn't call a variable "1" you would call it "one". It is so the compiler can differentiate between numbers and strings. I recommend just labeling the ids based on what it is going to do, makes it easier on you.

Upvotes: 1

Related Questions