I have three different blocks in my application. I'm trying to make a data binding.
Right now, I'm trying to figure out how to bind at least two layouts together.
This is part of my layout that I have to include:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<variable
name="textfieldview"
type="red.button.flipper_navigator.ui_logic.MainScreenLogic" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:stretchColumns="1">
........
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:text="@={textfieldview.link}"
android:id="@+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
android:hint="@string/maps_link"
android:inputType="text"
android:minHeight="35dp"
android:maxHeight="70dp"
android:importantForAutofill="no">
<requestFocus />
</EditText>
</TableRow>
</LinearLayout>
</layout>
this is where I'm including it:
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="mainview"
type="red.button.flipper_navigator.ui_logic.MainScreenLogic" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.MainScreen">
<include
android:id="@+id/flip_bar"
layout="@layout/flip_bar"
android:layout_width="match_parent"
android:layout_height="56dp"></include>
<!--this part where i'm including my important layout right now. -->
<include
android:id="@+id/text_field"
layout="@layout/text_field"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="144dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/flip_bar">
</include>
<!--part ends -->
<Button
android:id="@+id/generate"
android:onClick="@{mainview::generateBt}"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="80dp"
android:layout_marginBottom="320sp"
android:text="generate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
</Button>
<Button
android:id="@+id/Nav"
android:layout_width="wrap_content"
android:layout_height="58dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="315sp"
android:text="Start\n Navigation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
this is my main file code:
package red.button.flipper_navigator.ui
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import red.button.flipper_navigator.ui_logic.MainScreenLogic
import red.button.flipper_navigator.R
import red.button.flipper_navigator.databinding.ScreenMainBinding
import red.button.flipper_navigator.databinding.TextFieldBinding
//import red.button.flipper_navigator.databinding.TextFieldBinding
class MainScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ScreenMainBinding = DataBindingUtil.setContentView(this, R.layout.screen_main)
val viewModel = ViewModelProvider(this)[MainScreenLogic::class.java]
val exampleBinding: TextFieldBinding = TextFieldBinding.inflate(layoutInflater)
binding.mainview = viewModel
exampleBinding.textfieldview = viewModel
}
}
I'm trying to insert all my binding data into MainScreenLogic file using ViewModelProvider
I don't really understand what's wrong because it's been the 3 day, I'm trying, but nothing actually working.
This is MainScreenLogic file:
package red.button.flipper_navigator.ui_logic
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModel
class MainScreenLogic() : ViewModel() {
var link: String = ""
//val route = inputs(link)
fun generateBt(view: View) {
if (link == "ee") {
Log.d("button", "ok")
}
}
}
Link variable is always empty.
I just need to pass data from EditText to the variable, but I need to group everything in one layout using include or merge.
I tried to google first, of course, found something, but didn't manage to make it works.
Links that i tried:
View Binding - How do I get a binding for included layouts?
View Binding with 2 possible layouts, assign binding variable to 2 generated binding classes