For example i have view which have id :
<ImageButton
android:id="@+id/imageButtonStart"
android:layout_width="100dp"
android:layout_height="100dp" />
Does i need made private field with the same name, like this:
private ImageButton imageButtonStart;
It's the end of an era at Google: The company's Android mobile platform is dropping its long-running dessert-themed naming convention, switching instead to a simple numerical format. Beginning with the release of Android 1.5, the mobile popular platform Google acquired along with its creator Android Inc.
The m is here to indicate a member variable. It has 2 huge advantages: If you see it, you instantly recognize it as a member variable.
Do I need a private field with the same name?
No, the android:id
in the layout xml file does not require you to have a variable with the same name in your code.
But you need to use the exact name if you search for the id
View randomName = findViewById(R.id.imageButtonStart);
Naming is a lot of personal preference. You should chose names that clearly define what the named thing represents. And you should stay consistent with your naming scheme.
How long that name should be is debatable, some people would chose startBtn
over imageButtonStart
because it's shorter and less annoying in your code. If the fact that the button is an ImageButton
is irrelevant to your code, you don't need to name it imageButton
, just button
would be enough to make it's meaning clear.
Some people like to use names with a hierarchical scheme where the most important distinguisher comes first. Like the Java & Android package names com.google.market
instead of market.google.com
.
Instead of startButton
and endButton
, they'd chose buttonStart
and buttonEnd
. The reason behind this is that way you can use auto-completion more efficient. If you can't remember if you named your end button endButton
or stopButton
you need to look trough the whole list of proposals. Using the buttonXYZ
scheme you can start typing button
since you know that it's a button and will get only a few proposals.
Above is also a good example why you should stay consisten. If you name one button buttonStart
and the other clickablethingStop
the whole scheme is useless. Inconsistent naming requires more time to find the correct thing and can lead to bugs if you chose the wrong thing.
Another thing is the language you use for names. It is perfectly okay to use your own language if you write code just for yourself or people that speak the language. But once you want to share it with others stay with english. Even incorrect english naming is easier to read and understand than names in a language that you don't understand at all.
There are also a lot of other naming conventions out there: WP:Naming conventions
For example Android coding syle guide states:
It would be private ImageButton mButtonStart;
or private static sSingletonThing;
That convention is by the way only required to be used if you want to contribute to the Android sourcecode, you can write your own apps in any style.
The only naming convention that you should really follow is the general Java Class/Method/variable schema: (from above wikipedia link)
It starts to get very confusing for other people if they read your code and you have lowercase class names etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With