Android 2.3.3.
I have a question regarding naming the IDs in Android.
Let's say I have two buttons in Activity1 (save and cancel). I name them (IDs) as btnSave and btnCancel. Now I have Activity2, where in I have save and cancel buttons as well. Both does the same functionality. What will happen if I give the IDs as btnSave and btnCancel.
Will i face a problem while compiling? When I press, R.id. and ctrl+space, will I get two btnSave and btnCancel(s) to choose from?
And most importantly, Why should I name them differently, if I should?
If its only matter to easy way for writing in code, then
you can try something like, (writing button's name with either activity or layout xml file with prefix or suffix)
button_save_<activity_or_layout_name>
button_cancel_<activity_or_layout_name>
But at run time your button id always referred by the layout view. Which you are set into your Activity's setContentView()
.
Update:
Suppose in Activity2 you are using button with id of Activity1's layout then you can get NullPointerException as your button is not referenced in Current Activity2. (Because your Activity2 has different layout).
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