简体   繁体   中英

Unable to pass hardcoded string value to @BindingAdapter in Kotlin

Converted my Android app Java code to Kotlin and I'm using data binding to set custom font to TextViews. I used to pass font string from XML as below

app:customFont="@{'harmonia-semibold.ttf'}"

after converting @BindingAdapter to kotlin the above line is not working and its throwing expr or lambda expression expected, got ''' error. Replacing hardcoded string value with a getter method works perfect. Below is my binding adapter and not sure why its not taking hardcoded string

@JvmStatic 
@BindingAdapter("app:customFont")
fun setCustomFont(textView: TextView, font: String) {                       
      textView.typeface = Typeface.createFromAsset(textView.context.assets, font)
}

Thanks

See this snap I have faced the same problem. After a lot of trial & error I found the solution. This solution worked for me. Use ` sign before and after your hardcoded string.

instead of app:customFont="@{harmonia-semibold.ttf}"

<EditText
                android:id="@+id/etCNPwd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/edittext_underline_color"
                android:hint="@string/confirm_new_password"
                android:text="0000000000"
                android:inputType="textPassword"
                android:maxLength="10"
                android:textColor="@color/text_color24"
                android:textSize="@dimen/sp_15"
                bind:setUpPwdLayout="@{`Poppins-Regular.ttf`}"
                bind:addTextChangeListener="@{null}"/>

see the snap and find solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM