Below is XML equivalent of what I'm trying to achieve, but without XML
<Button
android:id="@+id/bt_start"
android:onClick="@{() -> vm.onClickedStart()}"
android:text='@{vm.btStartLbl}' />
so if I have:
ConstraintLayout cl = findViewById(R.id.cl);
Button btn = new Button(MyActivity.this);
btn.setText("How to Data Bind here?");
btn.setOnClickListener(new OnClickListener (){ /* how to databind here? */ });
cl.addView(btn);
how to databind it as equivalent as xml above? is it possible?
btn.setOnClickListener(() -> vm.onClickedStart());This assumes that you have avmin scope that has anonClickedStart()method. IOW, you do not "databind programmatically", but rather simply program.android:text='@{vm.btStartLbl}'how to bind it from Java?setText(vm.getBtStartLbl())(modifying that method name to match the actual one, as there are a few possibilities).vm.getBtStartLbl()will update mytextif variable changed, e.g:vm.label=oldVal;andgetBtStartLbl(){ return this.label; }ifvm.label = newValwill my button text get updated tonewVal?setText()again.