Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for IDs in Android

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?

like image 276
Vamsi Challa Avatar asked Dec 21 '22 09:12

Vamsi Challa


1 Answers

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).

like image 105
user370305 Avatar answered Dec 23 '22 01:12

user370305