I'm a newbie for android data binding.
I want to bind multiple SeekBars to a collection of float, such as
SeekBar1.progress <---> modelArray[0]
SeekBar2.progress <---> modelArray[1]
...
Since the progress of SeekBar is a Int type, I think it would be better to use Converter, and below is the converter code:
import android.databinding.InverseMethod
import android.util.Log
import kotlin.math.roundToInt
class Converter {
#InverseMethod("progressToFloat")
fun floatToProgress(value: Float): Int {
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
fun progressToFloat(value: Int): Float {
return value.toFloat()/100.0f
}
}
and the model structure looks like:
class Model {
val params by lazy {
ObservableArrayMap<Int, Float>()
}
//....
}
and my xml is following:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class="MyBinding">
<variable name="Converter" type="com.mypackage.model.Converter"></variable>
<variable
name="Converter"
type="com.mypackage.model.Converter"></variable>
<variable
name="model"
type="com.mypackage.model.Model"></variable>
</data>
...
<SeekBar
android:id="#+id/seekBar"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:progress="#={Converter.floatToProgress(model.params[0])}"/>
The problem is, every time I build it, it shows :
Found data binding errors.
****/ data binding error ****
msg:The expression converter.floatToProgress(modelParams0) cannot be inverted:
There is no inverse for method floatToProgress, you must add an #InverseMethod
annotation to the method to indicate which method should be used when using
it in two-way binding expressions
I already refer to lots of website, includes following:
https://medium.com/androiddevelopers/android-data-binding-inverse-functions-95aab4b11873
https://developer.android.com/topic/libraries/data-binding/two-way
But I still cannot find out what problem is. Could anyone give me some suggestion?
My development environment
macOS High Sierra,
Android Studio 3.2.1, with compileSdkVersion 28 and gradle 3.2.1
Update:
I also try to write Converter as following :
object Converter { // change to object
#InverseMethod("progressToFloat")
#JvmStatic fun floatToProgress(value: Float): Int { // Add static
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
#JvmStatic fun progressToFloat(value: Int): Float { // Add static
return value.toFloat()/100.0f
}
}
It still does not work.
Solution
Ok, I found the problem: My project does not use Kapt.
After add it to build.gradle, all works fine.
apply plugin: 'kotlin-kapt'
Also, I update my converter body as following:
object Converter {
#InverseMethod("progressToFloat")
#JvmStatic fun floatToProgress(value: Float): Int {
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
#JvmStatic fun progressToFloat(value: Int): Float {
val result = value.toFloat()/100.0f
Log.i("MODEL", "convert $value to $result(Float)")
return result
}
}
If the #JVMStatic removed, the project can compile successfully, but the binding does not work.
Thanks everyone who give my suggestion. I think I just ask a silly question.
Refer to:Databinding annotation processor kapt warning
Solution
Ok, I found the problem: My project does not use Kapt. After add it to build.gradle, all works fine.
apply plugin: 'kotlin-kapt'
Also, I update my converter body as following:
object Converter {
#InverseMethod("progressToFloat")
#JvmStatic fun floatToProgress(value: Float): Int {
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
#JvmStatic fun progressToFloat(value: Int): Float {
val result = value.toFloat()/100.0f
Log.i("MODEL", "convert $value to $result(Float)")
return result
}
}
If the #JVMStatic removed, the project can compile successfully, but the binding does not work.
Thanks everyone who give my suggestion. I think I just ask a silly question.
Refer to:Databinding annotation processor kapt warning
In my case the problem was that I was trying to make a custom TwoWay DataBinding Adapter
but my field in the ViewModel class was LiveData instead of MutableLiveData
Related
Following https://developer.android.com/topic/libraries/data-binding/two-way#converters,
I am trying to implement a data converter for two-way data binding in android.
The functionality of the converter:
Given a 10 digit phone number, add country code to the phone number.
XML code:
<data>
<import type="<package_name>.PhoneNumberStringConverter" />
<variable
name="model"
type="<package_name>.MyViewModel" />
</data>
<androidx.appcompat.widget.AppCompatEditText
android:text="#={PhoneNumberStringConverter.addExtension(model.storeDetailsEntity.storePhoneNumber)}"
... // Other irrelevant attributes are not shown
/>
Converter:
object PhoneNumberStringConverter {
#InverseMethod("addExtension")
#JvmStatic
fun removeExtension(view: EditText, oldValue: String, value: String): String {
return value.substring(3)
}
#JvmStatic
fun addExtension(view: EditText, oldValue: String, value: String): String {
return "+91$value"
}
}
When I add the converter in the XML, the build is failing.
Getting MyLayoutBindingImpl not found. Binding class generation issues.
Note:
1. Two-way data binding is working as expected, the issue is only with the converter.
Already referred:
Two-way data binding Converter
Edit:
Thanks to #Hasif Seyd's solution.
Working code:
PhoneNumberStringConverter:
object PhoneNumberStringConverter {
#JvmStatic
fun addExtension(value: String): String {
return "+91$value"
}
#InverseMethod("addExtension")
#JvmStatic
fun removeExtension(value: String): String {
return if (value.length > 3) {
value.substring(3)
} else ""
}
}
XML:
android:text="#={PhoneNumberStringConverter.removeExtension(model.storeDetailsEntity.storePhoneNumber)}"
Changed addExtension to removeExtension.
There are some issues in the code. Since you are using two way binding convertors,
first issue is you are trying to directly call the inverse binding adapter in the xml , but as per wat i see in ur convertor definition , binding adapter is removeExtension, so u have to assign that in the xml directly.
Another possible reason could be because of having parameters view and oldValue , which are not required , if you remove those two parameters from the Binding Functions , your code would compile successfully
I am implementing two way data binding on custom View. I followed the official android developers but still can't make it work. I have a knob that controlls integer value inside the value property.
class ControlKnob(context: Context, attributeSet : android.util.AttributeSet) : RelativeLayout(context, attributeSet), IUIControl {
companion object {
#JvmStatic
#BindingAdapter("value")
fun setValue(knob : ControlKnob, value : Int) {
if(knob.value != value) {
knob.value = value
}
}
#JvmStatic
#InverseBindingAdapter(attribute = "value")
fun getValue(knob : ControlKnob) : Int {
return knob.value
}
#JvmStatic
#BindingAdapter("app:valueAttrChanged")
fun setListeners( knob : ControlKnob, attrChange : InverseBindingListener) {
knob.setOnProgressChangedListener {
attrChange.onChange()
}
}
}
var value : Int = -1
set(value) {
field = value
valueView.text = stringConverter.invoke(value)
}
....
....
}
Inside layout i use it like this:
<cz.abc.def.package.controls.ControlKnob
android:id="#+id/knob"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="0"
app:value="#={viewModel.value}"
app:label="Knob" />
And my view model:
#Bindable
fun getValue() : Int {
return someValue
}
fun setValue(value : Int) {
someValue = value
}
But still i can't compile it. I get
Cannot find a getter for cz.abc.def.package.controls.ControlKnob app:value that accepts parameter type 'int'
If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.
What could be the cause of this ?
I figured it out. It turned out that it is not problem with the code. I was missing the apply plugin: 'kotlin-kapt' in the gradle build file. After i added this line into build.gradle in the module it worked.
Maybe your listener binding adapter is wrong? Per the documentation, the event listener BindingAdapter value should be "android:valueAttrChanged" and you have "app:valueAttrChanged".
I have a function which formats some text
fun String.formatTo(): String {
if (this.isNotEmpty()) {
val value = this.toDouble()
return "%.02f".format(value)
}
return ""
}
And I want to apply this fun to my textView, using databinding, so I called in textView android:text="#{viewModel.text.formatTo()}", importing class in data of my layout
<data>
<import type="com.project.utils.extensions.ExtKt"/>
<variable
name="viewModel"
type="com.project.ViewModel" />
</data>
But I've got an error throw building:
Found data binding errors.
****/ data binding error ****msg:cannot find method formatTo() in class java.lang.String
What is a problem?
Create an object named ExtKt (or anything you want) and define your extension function in it and annotate it with #JvmStatic like below
#JvmStatic
fun String.formatTo(): String {
if (this.isNotEmpty()) {
val value = this.toDouble()
return "%.02f".format(value)
}
return ""
}
Update
android:text="#{ExtKt.formatTo()}"
Databinding is still Java modules, so some features of kotlin like extension functions can't be used there. The only thing you can do here - create specific function in your ViewModel class.
class ViewModel {
val text: String
...
fun getDisplayText(): String = text.formatTo()
}
May be you want to use calculated properties.
val displayText: String get() = text.formatTo()
Anyway, your xml call will look like following:
android:text="#{viewModel.displayText}"
Consider use MediatorLiveData:
class ViewModel(
val list: MutableLiveData<List<String>> = MutableLiveData<List<String>>()
) {
val listStr = MediatorLiveData<String>()
init {
listStr .addSource(list, Observer {
listStr .postValue(ViewModel.joinList(it))
})
}
companion object {
#JvmStatic fun joinList(list: List<String>): String {
return list.joinToString(separator = ", ")
}
}
}
And than in the xml:
<TextView
android:id="#+id/items"
android:text="#{viewModel.listStr}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I'm trying to use Kotlin extension methods inside Android's databinding. For example; calling an onclick handler. So I've made this code:
posttest_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<import type="android.view.View"/>
<import type="com.example.test.post.posttest.PostTestItemViewModelExtensionKt" />
<variable
name="viewModel"
type="com.example.test.post.posttest.PostTestItemViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:clickable="true"
android:onClick="#{(view) -> viewModel.clicked(view)}"
>
[...]
PostTestItemViewModel.kt
open class PostTestItemViewModel : ViewModel() {
val postTitle = MutableLiveData<String>()
val postBody = MutableLiveData<String>()
/**
* Binds the required properties/entities to this ViewModel
*/
fun bind(post: Post) {
postTitle.value = post.title
postBody.value = post.body
}
}
PostTestItemViewModelExtension.kt
fun PostTestItemViewModel.clicked(v: View) {
this.postTitle.value = "clicked"
}
So when I place the clicked method inside the viewmodel, it works perfectly the way it should be. However, when I create it as an extension method, I get the following error on compilation:
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
cannot find method clicked(android.view.View) in class ...PostItemViewModel
I've tried different things already, such as changing the android:onclick tag to PostTestItemViewModelExtensionKt instead of viewModel. Unfortunately all the things don't seem to work. So it looks like the extension method is getting generated after the databinding takes place. Is there a way around this or am I still doing something wrong? Or is it just not possible to bind extension methods?
I'm using Kotlin version 1.2.71, gradle 3.2.0 and have the databinding { enabled = true } and kapt { generateStubs = true } added to my .gradle, and have the plugings kotlin-android, kotlin-android-extensions and kotlin-kapt defined.
Unfortunately you can't use extension methods as onClick callbacks.
Extension methods in Kotlin are created as Java static methods while the Android framework is expecting an instance method.
Note that in Android Studio you can decompile the Kotlin classes as Java to see the generated Java code.
So, today(2022) I had the same use case in one of my projects and i was able to figure out a way to implement custom click listeners for android views using data binding and custom adapters.
The use case is :
Click event should not be triggered twice or to prevent accidental clicks from the user
I created a file called ViewExtensions.kt and added the following code
class DebouncingOnClickListener(
private val intervalMillis: Long,
private val doClick: (() -> Unit)
) : View.OnClickListener {
override fun onClick(v: View) {
if (enabled) {
enabled = false
v.postDelayed(ENABLE_AGAIN, intervalMillis)
doClick()
}
}
companion object {
#JvmStatic
var enabled = true
private val ENABLE_AGAIN =
Runnable { enabled = true }
}
}
#BindingAdapter("singleClick")
fun View.setSingleClick(doClick: () -> Unit) =
setOnClickListener(
DebouncingOnClickListener(
intervalMillis = 5000, //5ms delay for click event
doClick = doClick
)
)
The debouncing click is used to defer the click for the given time, and in the xml called the click event like below
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
app:singleClick="#{()->fragment.clicked()}" />
Now I'm able to listen for click events on both fragment and in the viewmodel and the click is deferred for the given amount of time.
Hence the user cannot click the view accidentally multiple times.
References:
https://proandroiddev.com/ensure-single-click-on-android-butterknife-did-it-right-48ef56153c78
I've been moving into using Room, and I've run into a blocking issue. I've gone through and fixed all of the compile-time checks from the Room library, but am now encountering the following error:
Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
This appears twice at compile time with no evidence of which class this comes from, but I was able to figure out (by removing classes from the Database) that this was one of the files. I'm assuming it has something to do with the Primary Key being a string instead of an Int (this is one of two classes that uses this), but nothing in the documentation indicates what the issue would be, and in fact the documentation shows that strings are valid Primary Keys.
#Entity(tableName = "inspections")
data class Inspection(
#SerializedName("id")
var id: Int = 0,
...
// Rest of code left off for brevity, found to not be related to the issue.
I've tried a few things to try and get around this.
Remove the data attribute of this class to make it a normal POKO
Remove the variables from the default constructor, and place them into the class
Remove the Ignore from the empty constructor (note, this causes a different issue, Room cannot pick a constructor since multiple constructors are suitable - the Ignore annotation on a default constructor gets around this.) This is the part which perplexes me the most - removing this says "multiple constructors are valid", keeping it says "no constructors are valid".
Updated: Adding a few more relevant code snippets from my project.
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
.....
implementation 'android.arch.persistence.room:runtime:1.0.0-alpha9-1'
implementation 'android.arch.persistence.room:rxjava2:1.0.0-alpha9-1'
kapt 'android.arch.persistence.room:compiler:1.0.0-alpha9-1'
Database class
#Database(entities =
arrayOf(Account::class, Category::class,
Inspection::class, InspectionForm::class,
InspectionFormItem::class, InspectionFormsStructure::class,
InspectionItemPhoto::class,
InspectionItem::class, LineItem::class,
LocalPhoto::class, Rating::class,
Structure::class, SupervisoryZone::class,
Upload::class, User::class),
version = 16)
#TypeConverters(Converters::class)
abstract class OrangeDatabase : RoomDatabase() {
abstract fun inspectionDao(): InspectionDao
abstract fun localDao(): LocalDao
abstract fun ratingsDao(): RatingsDao
abstract fun structureZoneDao(): StructureZoneDao
abstract fun userAccountDao(): UserAccountDao
}
Converters
class Converters {
#TypeConverter
fun fromTimestamp(value: Long?): Date? {
return if (value == null) Date() else Date(value)
}
#TypeConverter
fun dateToTimestamp(date: Date?): Long? {
return date?.time ?: 0
}
#TypeConverter
fun fromStringToArray(value: String?): Array<String>? {
return value?.split(",")?.toTypedArray() ?: arrayOf()
}
#TypeConverter
fun stringToStringArray(strings: Array<String>?): String? {
return strings?.joinToString(",") ?: ""
}
}
Another data class
#Entity(tableName = "users")
data class User(
#PrimaryKey
#SerializedName("id")
var id: Int = 0,
...
// Rest of code left off for brevity, found to not be related to the issue.
UserPermissions class:
data class UserPermissions(
#SerializedName("id")
var pid: Int = 0,
...
// Rest of code left off for brevity, found to not be related to the issue.
The problem in your case is, that if you have nullable values Kotlin will generate several constructors for each possible constructor.
That means that you have to define a default constructor and fill it with default values.
If you want to have another one which should be ignored you should make sure to use the parent constructor with all those parameters.
Example:
#Entity(tableName = "inspections")
data class Inspection(
#SerializedName("id")
var id: Int = 0,
#PrimaryKey
#SerializedName("guid")
var guid: String = "",
#SerializedName("score")
var score: Double = 0.0,
#SerializedName("notification_sent_at")
var notificationSentAt: Date = Date(),
var wasUploaded: Boolean = false) {
#Ignore
constructor() : this(0, "", 0.0, Date(), false)
}
In this case only two constructors will be generated "under the hood". If you have nullable values you will have all possible constructors available.
Example:
data class Test(var id: Int = 0, var testString: String? = null, var testBool : Boolean? = null) {
constructor(0)
}
generates
constructor(var id:Int)
constructor() : this(0)
constructor(var id:Int, var testString: String)
constructor(var id:Int, var testBool: Boolean)
constructor(var id:Int, var testString: String, var testBool : Boolean)
// .. and so on
Since you'r looking for an official documentation, you may want to look at Overloads Generation.
After testing your class which works flawlessly i found in another post that you have to check if you used apply plugin: 'kotlin-kapt' in your Gradle.
Double check that you've valid type converters for your Date class. I wrote that issue longer time ago.
After recoding your stuff above it worked just fine by adding a UserPermissions class like that:
data class UserPermissions(var permissionid: String)
Edit: After using your UserPermission class everything worked just fine. Please take care if you use the proper import (util.Date instead of sql.Date for example).
Another problem is that your using an old very buggy library of room.
The current version (while writing this) is
implementation "android.arch.persistence.room:runtime:1.0.0-beta2"
kapt "android.arch.persistence.room:compiler:1.0.0-beta2"
implementation "android.arch.persistence.room:rxjava2:1.0.0-beta2"
I wrote an issue long time ago
The issue was extremely difficult to debug and harder to reproduce, but I found the issue. I was using an #Embedded object, but the result that was going in was actually a List of that object. This was giving trouble to the automatic Embed task, and there wasn't a perfect Converter that could be written for it.
#SerializedName("range_choices")
#Embedded
var rangeChoices: List<RangeChoice>? = null,
I had to annotate that with #Ignore and instead, I'll be saving the results of this list to its own table, now the new table range_choices.
Your Primary keys should be like given below using Annotation Use-site Targets
#field:PrimaryKey #field:SerializedName("guid") var guid: String = ""
and
#field:PrimaryKey #field:SerializedName("id") var id: Int = 0
Try to avoid using nullable values and make everything have some kind of default value. That's the simpliest way to solve this issue.
If you really want to use them, then you may create a constructor, containing all of them.