Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What id name convention is good for Kotlin android extensions

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?

like image 924
Daniel Gomez Rico Avatar asked Apr 26 '17 03:04

Daniel Gomez Rico


People also ask

How do I name my Kotlin file?

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 .

What is Kotlin Android extensions?

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'


2 Answers

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 .../>
like image 124
David Avatar answered Oct 23 '22 05:10

David


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

like image 2
Luis Avatar answered Oct 23 '22 06:10

Luis