Issue
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:
- 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"
- 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?
Solution
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 .../>
Answered By - David
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.