14

Using Kotin android extensions I can avoid using findViewById, but Im not sure how to name the ids to use it propertly.

I found two options are:

  1. Use simple names for ids but then I can get in trouble with espresso if I use it with fragments:

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: .../mainLayout' matches multiple views in the hierarchy.

It is because I have two fragments inside a TabLayout with same ids:

<LinearLayout android:id="@+id/mainLayout"
  1. Name with owner: "@+id/loginMainLayout" and "@+id/signUpMainLayout"

But then I will have to use variables like signUpMainLayout.doSomething().

Note: I dont like the use of _ in this case since that's not a good code style.

What other options are?

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
  • I don't see the reason for not using `_` ? your ref code style is for java, but our layout file is xml – Trung Le Apr 26 '17 at 04:02
  • 2
    @TrungLe with kotlin extensions they create extension functions with the name of your ids to reference the views, so the code will be full of `username_textfield.text = "this text"` and that looks completely out of java/kotlin code styles. – Daniel Gomez Rico Apr 26 '17 at 14:04

2 Answers2

3

I can't recognize why you don't use "@+id/loginMainLayout" and "@+id/signUpMainLayout" when the name is in lowerCamelCase that is common in Kotlin and Java. The use case will be signUpMainLayout.doSomething() as you said.

Anyway It's a good practice to use unique names for id's in whole app. it's not because of Espresso but mainly to know where the view associated with an ID when you see the name of ID. It's not hard if you use this style. Example:

In fragment_contacts:

<TextView id="+id/contactNameText 
android:text="John Smith" .../>

<ImageView id="+id/contactUserImage .../>

Assert: There is Image in contactUserImage because to know it's an ImageView.

In fragment_settings:

<TextView id="+id/settingsNotificationText 
android:text="Turn notifications on/off" .../>

<checkBox id="+id/settingsNotificationCheck .../>
Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
David
  • 2,129
  • 25
  • 34
  • 1
    What I dont like about that idea is that the code becomes enormous for using those variables, think about a view like `activity_expert_profile` for your kind of users that are "experts" (you may habe `activity_normal_profile` for the others), then a username TextView may be called `expertProfileUserNameTextView`, as you can see the variable name is ultra BIG, that's just what make me feel bad about it. – Daniel Gomez Rico May 18 '17 at 16:05
  • And that's not even touching the issue, because you may have multiple screens with expert profile. So it easily becomes settingsExpertProfileUserNameSimpleTextView – Stan Oct 26 '18 at 07:13
2

in my case, I have been working using this convention https://jeroenmols.com/blog/2016/03/07/resourcenaming/, but without the underscores and in camel case convention.

If you notice when you drag a control to a view, within android studio, it names the id using the camel case convention.

And although the names of the variables may be a bit large, you can always use val or var in the declaration of the variable.

regards

Luis
  • 41
  • 1
  • 7