Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use companion objects on xml layout?

Tags:

I am trying to use a companion object property inside the layout but the compiler doesn't recognise it.

Kotlin Class

class MyClass {   companion object {     val SomeProperty = "hey"   } } 

XML Layout

<layout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:app="http://schemas.android.com/apk/res-auto"   xmlns:fancy="http://schemas.android.com/tools">    <data>     <import type="package.MyClass"/>   </data>    <TextView     android:layout_width="match_parent"     android:layout_height="match_parent"     android:text="@{MyClass.Companion.SomeProperty}"/>  </layout> 

And I got this error:

e: java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Could not find accessor package.MyClass.Companion.SomeProperty file:/path/to/my/layout.xml loc:21:67 - 21:103 ****\ data binding error ****      at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)     at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)     at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)     ... Caused by: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Could not find accessor package.MyClass.Companion.SomeProperty file:/path/to/my/layout.xml loc:21:67 - 21:103 ****\ data binding error ****      at android.databinding.tool.processing.Scope.assertNoError(Scope.java:112)     at android.databinding.annotationprocessor.ProcessDataBinding.doProcess(ProcessDataBinding.java:101)     at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:65)     ... 

I've tried to use companion instead of Companion, but no luck.

Is it possible to use companion objects on xml layout with databinding? How can I proceed? Thanks in advance for any help :)

like image 857
melanke Avatar asked Jun 20 '18 14:06

melanke


People also ask

What is the use of companion object?

companion object is how you define static variables/methods in Kotlin. You are not supposed to create a new instance of Retrofit / ApiService each time you execute a request, however.

How do you use companion objects in Java?

Declaring a companion objectIf we declare the object inside a class, we have an option to mark it as a companion object. In terms of Java, the members of the companion object can be accessed as static members of the class. Marking an object as a companion allows us to omit the object's name while calling its members.

What is difference between companion object and object?

A companion object is an object that's declared in the same file as a class , and has the same name as the class. A companion object and its class can access each other's private members. A companion object's apply method lets you create new instances of a class without using the new keyword.

How do I access companion object in XML?

Companion object are already instantiated, therefore you can access the instance directly. Instead of using <import> (which is the natural translation from Java), we need to use <variable>, because we actually want to use the (already instantiated) Companion object into our XML Layout.

How to declare a companion object inside an existing class?

The companion object can be declared within an existing class and the functions or parameters can be defined inside the companion object declaration. As you can see above, we can declare a companion object inside an existing class. We can also have an explicit name for the companion object that’s declared for enhanced readability.

How to create a companion object in Kotlin?

To create a companion object, you need to add the companion keyword in front of the object declaration. Similarly, you can put some variables in the companion object and access it with the help of the class name. The following is an example of the same: This is all about the companion object in Kotlin.

What is a companion object in Java?

A few important points to remember while defining companion objects are: Companion objects cannot be declared outside a class. Companion objects can be used similar to static classes in other programming languages like Java and C#. These objects are common in all instances of the class.


1 Answers

In order to access Companion object attributes and methods, it is NOT required to have an instance of the Parent object. Companion object are already instantiated, therefore you can access the instance directly.

Instead of using <import> (which is the natural translation from Java), we need to use <variable>, because we actually want to use the (already instantiated) Companion object into our XML Layout.

Import your Companion object as follow

Given Kotlin class:

package com.example.project  class MyViewModel {     companion object {         // it is only working with val and var         // const val wouldn't work         val MAX_LENGTH = 10     } } 

Layout:

    <data>         <!-- Declare your "variable" that hold the Companion object itself -->         <variable name="myViewModelStatic" type="com.example.project.MyViewModel.Companion" />     </data>      <!-- then use the myViewModelStatic to access "static" properties of MyViewModel -->     <EditText     ...     android:maxLength="@{ myViewModelStatic.MAX_LENGTH }"     /> </layout> 

Fragment:

class MyFragment {     ...     onViewCreated(...) {          // now bind the companion object to the variable declared in the XML          binding.myViewModelStatic = TransferUseCase.Companion     }     ... } 
like image 180
sonique Avatar answered Sep 21 '22 02:09

sonique