View Binding — Event Handling

Method references

public class WelcomActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WelcomMessage welcomMessage =
new WelcomMessage("Name : Welcome to Data Binding", "Version :2.0.1");
ActivityWelcomBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_welcom);

}


public void OpenMainActivity(View view) {
Toast.makeText(this,"Main Activity",Toast.LENGTH_LONG).show();
}

public void openObserverBinding(View view) {
Toast.makeText(this,"Observer Activity",Toast.LENGTH_LONG).show();
}

public void openAdapterBinding(View view) {
Toast.makeText(this,"Adapter Activity",Toast.LENGTH_LONG).show();
}

}
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>
<variable
name="welcomActivityPresenter"
type="com.example.view_binding.WelcomActivity"
/>
</data>


<LinearLayout
android:layout_below="@+id/version"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:onClick="@{welcomActivityPresenter::OpenMainActivity}"
android:text="Main Activity!"
></Button>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/version"
android:onClick="@{welcomActivityPresenter::openAdapterBinding}"
android:text="Adapter Binding!"
></Button>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/version"
android:onClick="@{welcomActivityPresenter::openObserverBinding}"
android:text="Observer Activity!"
></Button>
</LinearLayout>

</layout>
viewDataBinding.setWelcomActivityPresenter(WelcomActivity.this);

NOTE: Until an unless you won’t add the above value you wont be able to see toast after dispatching the event on button click

Listener bindings

public class AddUserPresenter {
private OnUserAddListner onUserAddListner;
private List<UserDetails> userDetails = new ArrayList<>();

public AddUserPresenter(OnUserAddListner onUserAddListner) {
this.onUserAddListner = (OnUserAddListner) onUserAddListner;
}

public void addUserToList(String name, String age) {
onUserAddListner.updateListView(new UserDetails(name, age));
}
}
public interface OnUserAddListner {
void updateListView(UserDetails userDetails);
}
public class UserDetails {
private String name;
private String age;

public UserDetails(String name, String age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public String getAge() {
return age;
}

@NonNull
@Override
public String toString() {
return "Name :" + name + "and Age :" + age;
}
}
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>
<variable
name="addUser"
type="com.example.view_binding.presenter.AddUserPresenter"
/>
</data>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
/>

<EditText
android:id="@+id/edtAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{()->addUser.addUserToList(edtName.getText().toString(),edtAge.getText().toString())}"
android:text="ADD User"
/>
</LinearLayout>

</layout>
android:onClick="@{
()-> addUser.addUserToList(
edtName.getText().toString(),
edtAge.getText().toString()
)
}"
public class MainActivity extends AppCompatActivity implements OnUserAddListner {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AddUserPresenter addUserPresenter = new AddUserPresenter(this);

ActivityMainBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);


viewDataBinding.setAddUser(addUserPresenter);

}


@Override
public void updateListView(UserDetails userDetails) {
Toast.makeText(this,"Value Recived",Toast.LENGTH_LONG).show();
}
}

--

--

Mobile Application Developer | Flutter Developer | Project Manager | Team Leader | Scrum Certified | PMI APC Appeard | Writing (starting soon)

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store