[Huawei Developers] Freemind R:
This article is originally from HUAWEI Developer Forum

Forum link: https://forums.developer.huawei.com/forumPortal/en/home​

In this post, we will learn Kotlin with data binding in Android. It gives you the ability to communicate between your view and model. It keeps the code clean and sorted.

Note: Configuration part check previous article

How to Use Data Binding Library with Kotlin – A Step By Step Guide

It’s a library that allows you to bind the data of your models directly to the xml views in a very flexible way.

Kotlin was recently introduced as a secondary ‘official’ Java language. It is similar to Java in many ways but is a little easier to learn and get to grips with. Some of the biggest companies have adopted Kotlin and seen amazing results.

If you want to use data binding and Kotlin, here are a few things to keep in mind:

· Data binding is a support library, so it can be used with all Android platform versions all the way back to Android 2.1 (API level 7+).

· To use data binding, you need Android Plugin for Gradle 1.5.0-alpha1 or higher. You can see here how to update the Android Plugin for Gradle.

· Android Studio 3.0 fully supports kotlin

First of all, create an Android Studio project and add a dependency for Kotlin and few changes for your App level build.gradle

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051344.28072358682830255429944303212528:50510529085146:2800:048693C76BD8746B9BA48542BAA6D57609C210868BD3086ABC2E0DF853BFE47A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051344.28072358682830255429944303212528:50510529085146:2800:048693C76BD8746B9BA48542BAA6D57609C210868BD3086ABC2E0DF853BFE47A.png[/IMG]

· Add below line into Root level build.gradle

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

Data Binding:

· Android offers support to write declarative layouts using data binding.

· This minimizes the necessary code in your application logic to connect to the user interface elements.

· The usage of data binding requires changes in your layout files. Such layout files starts with a layout root tag followed by a data element and a view root element.

· The data elements describe data which is available for binding. This view element contains your root hierarchy similar to layout files which are not used with data binding.

· References to the data elements or expressions within the layout are written in the attribute properties using the @{} or @={}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051404.92728951657336402953370270226436:50510529085146:2800:5431EEA3647E17C4728D83E8F09F2BFDC9389D268DD33567D7234A376A413DA0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051404.92728951657336402953370270226436:50510529085146:2800:5431EEA3647E17C4728D83E8F09F2BFDC9389D268DD33567D7234A376A413DA0.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051411.99957485602237904166493893132868:50510529085146:2800:FA5DD0F4840FB7EB952418BE6A42DC85160DF6E8DE585F45A51E0E504993CC2A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051411.99957485602237904166493893132868:50510529085146:2800:FA5DD0F4840FB7EB952418BE6A42DC85160DF6E8DE585F45A51E0E504993CC2A.png[/IMG]

1. The user variable within data describes a property that may be used within this layout

2. Normal view hierarchy.

3. Binding Input data.

Code Implementation:

1.SignInActivity.kt

class SignInActivity : AppCompatActivity() {
private var mInstance: HiAnalyticsInstance? = null
private lateinit var mDataBinding: ActivitySigninBinding
var viewmodel: SignInViewModel? = null
var customeProgressDialog: CustomeProgressDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_signin)
viewmodel = ViewModelProviders.of(this).get(SignInViewModel::class.java)
mDataBinding.setLifecycleOwner(this);
mDataBinding?.viewmodel = viewmodel
customeProgressDialog = CustomeProgressDialog(this)
initObservables()
init();
}

private fun initObservables() {
viewmodel?.userLogin?.observe(this, Observer { userInfo ->
Toast.makeText(this, "welcome, ${userInfo?.email}", Toast.LENGTH_LONG).show()
val bundle = Bundle()
bundle.putString("email", userInfo?.email)
bundle.putString("password", userInfo?.password)
mInstance!!.onEvent(HAEventType.SIGNIN, bundle)
val intent = Intent(this, ProfileScreen::class.java)
startActivity(intent)
})
viewmodel?.progressDialog?.observe(this, Observer {
if (it!!) customeProgressDialog?.show() else customeProgressDialog?.dismiss()
})
}

private fun init() {
HiAnalyticsTools.enableLog()
mInstance = HiAnalytics.getInstance(this)
mInstance?.setAnalyticsEnabled(true)
mInstance?.regHmsSvcEvent()
}
}

Viewmodel Class:

· The view model coordinates the view's interaction with the model.

· It may convert or manipulate data so that it can be easily consumed by the view and may implement additional properties that may not be present on the model.

· The view model may define logical states that the view can represent visually to the user.

class SignInViewModel(application: Application) : AndroidViewModel(application) {
var btnSelected: ObservableBoolean? = null
var email: ObservableField<String>? = null
var password: ObservableField<String>? = null
var userLogin: MutableLiveData<UserInfo>? = null
var progressDialog: SingleLiveEvent<Boolean>? = null

init {
btnSelected = ObservableBoolean(false)
email = ObservableField("")
password = ObservableField("")
userLogin = MutableLiveData()
progressDialog = SingleLiveEvent<Boolean>()

}

fun onEmailChanged(s: CharSequence, start: Int, befor: Int, count: Int) {
btnSelected?.set(password?.get()!!.length != 0)
}

fun onPasswordChanged(s: CharSequence, start: Int, befor: Int, count: Int) {
btnSelected?.set(s.toString().length >= 8)
}

fun onLoginClick() {
progressDialog?.value = false
val userInfo = UserInfo(email?.get(), password?.get())
userLogin?.value = userInfo
}
}

Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051434.26241131013554383168866974818200:50510529085146:2800:25EB7232EE838A9C1F381B5303B5B545482A781522C1096073B34A4D1DFEE9E1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051434.26241131013554383168866974818200:50510529085146:2800:25EB7232EE838A9C1F381B5303B5B545482A781522C1096073B34A4D1DFEE9E1.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051501.41318464524026742149630892581705:50510529085146:2800:6E8BAC996E04B1A1E75118DBEBF7AA9136FBC106D97913AF8EB89D7B3898712F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051501.41318464524026742149630892581705:50510529085146:2800:6E8BAC996E04B1A1E75118DBEBF7AA9136FBC106D97913AF8EB89D7B3898712F.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051507.05901250217293230407124233710610:50510529085146:2800:6DEAD92FCF695A2B1F805A343DCF1A3340E60D4AB149ECDDC9F82AB3A11FEE36.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420051507.05901250217293230407124233710610:50510529085146:2800:6DEAD92FCF695A2B1F805A343DCF1A3340E60D4AB149ECDDC9F82AB3A11FEE36.png[/IMG]

Overview of application:

Event Analysis:

· Event Analysis to collect data about interactions with your content.

· An Event hit includes a value of each component and these values are displayed in report.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420052234.26090164679031777200999416379716:50510529085146:2800:998C9D94EF1ADF3C476305F101AA6BFB7B553842A6F056B583359D309C0EE6A6.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200420052234.26090164679031777200999416379716:50510529085146:2800:998C9D94EF1ADF3C476305F101AA6BFB7B553842A6F056B583359D309C0EE6A6.png[/IMG]

Any questions about this, you can acquire answers from HUAWEI Developer Forum.