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:
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"
"@+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?
Naming. If a source file contains only a single top-level class, the file name should reflect the case-sensitive name plus the . kt extension. Otherwise, if a source file contains multiple top-level declarations, choose a name that describes the contents of the file, apply PascalCase, and append the .
The Kotlin Android Extensions is a compiler plugin that allows you to access views from your XML directly in activities, fragments and views using what they refer to as synthetic properties. To use it, ensure that the plugin is enabled in your module's build.gradle file: apply plugin: 'kotlin-android-extensions'
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 .../>
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
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