Do I need a personal field with the same name?
No, android:id in the layout XML file does not require a variable with the same name in your code.
But you need to use the exact name if you are looking for id
View randomName = findViewById(R.id.imageButtonStart);
Naming is a lot of personal preference. You must choose names that clearly define what a named thing is. And you must stay in accordance with your naming scheme.
How long this name should be debatable, some people chose startBtn over imageButtonStart because it is shorter and less annoying to your code. If the fact that the ImageButton button ImageButton not related to your code, you do not need to call it ImageButton , just a button will be enough to make it understandable.
Some people like to use hierarchical names, with the most important distinguishing feature in the first place. Like the Java and Android package names com.google.market instead of market.google.com .
Instead of startButton and endButton they selected buttonStart and buttonEnd . The reason for this is that you can use autocomplete more efficiently. If you cannot remember whether you specified your endButton or stopButton end button, you need to look at the entire list of offers. Using the buttonXYZ scheme, you can start typing button , since you know that it is a button and you only get a few suggestions.
The above is also a good example of why you should stay the same. If you call one button buttonStart and the other clickablethingStop , the whole scheme is useless. Inconsistent naming takes more time to find the right thing and can lead to errors if you choose the wrong thing.
Another thing is the language you use for names. It is perfectly normal to use your own language if you write code only for yourself or people who speak that language. But as soon as you want to share it with others, stay with English. Even a wrong English name is easier to read and understand than names in a language that you donβt understand at all.
There are also many other naming conventions: WP: Naming Conventions
For example, Android syle coding guide :
- Non-public names of non-static fields begin with m.
- Static field names begin with s.
- Other fields begin with a lowercase letter.
- Open static end fields (constants) - ALL_CAPS_WITH_UNDERSCORES.
This will be a private ImageButton mButtonStart; or private static sSingletonThing;
This agreement, by the way, is required only for use, if you want to contribute to the Android source code, you can write your own applications in any style.
The only naming convention you should follow is the general Java schema / method / variable: (on top of the wikipedia link)
- Class Names: UpperCamelCase
- Method Names: lowerCamelCase
- Variable Names: lowerCamelCase
- Constants: ALL_CAPS
It starts to get very confused for other people if they read your code, and you have lowercase names, etc.