EventBus for better Communication Between Components in Android 
Tags: android, java
Published: 2020-09-11 19:22:42

__EventBus__ is the number 1 event library for android and java. __EventBus__ uses the publisher and subscriber pattern for loose coupling which means the possibility of errors will be less. __EventBus__ simplifies the communication between different components for example communications between different levels of activities or even services. You need very less or no setup to use __EventBus__ in your existing project. 

EventBus provides a convenient annotation-based API which makes it fast and the performance is increased. EventBus is used by many popular apps, apps which has 1 billion-plus download which shows how EventBus popular.

In this tutorial, we will see how we can get started using EventBus in an android project. Before jumping in the code let's see what we will be going to make. To better understanding EventBus we will make an app that allows us to add items to the cart and show the total count of the items. See below how our app will work.

![EventBus Android](https://dev-to-uploads.s3.amazonaws.com/i/7ugbmbpi6tnmid1jg0k9.gif)

Let's get started.

Add EventBus Dependency in Android Studio
First, we need to add EventBus Dependency in the project. Open your build.gradle and add the dependency.

```gradle
implementation 'org.greenrobot:eventbus:3.2.0'
```
Hit the sync button and move to the next step.

## Make a POJO class for EventBus
The next step is to make a Java object class to pass in the EevntBus Subscribe method and to be used later.

For our example app, we will make a class with the name of CartEvent.java with a string field for cart items.

See the below code for better understanding.

```java
public class CartEvent {
    public String cartItem;
  
    public CartEvent(String cartItem) {
        this.cartItem = cartItem;
    }
}
```
Move to the next step.

See also: [Android Libraries for Beginners](https://www.akshayrana.in/2020/08/top-10-libraries-for-android-developers.html)

## Make UI for the App
In the first activity, we have a textview to show the total count of the cart item.

And a button to open second activity to add items in the cart.

See the below code to understand.
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cart Item: 0"
        android:id="@+id/cartTextView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="50sp"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Items"
        android:id="@+id/button"
        android:onClick="open"
        app:layout_constraintTop_toBottomOf="@id/cartTextView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
```
Now make the second activity to show the items list and add a method for the button click.

We made simple cart items. We add 3 items but in the real world application, you should use recyclerview or any other view to make a list of items.

Here is the second activity XML code.
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15sp"
    tools:context=".MainActivity2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Item 1"
        android:layout_marginTop="25sp"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addItem1"
        android:text="Add to cart"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/item1"
        app:layout_constraintBottom_toBottomOf="@id/item1"
        android:onClick="addItemToCart"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/item1"
        android:text="Item 2"
        android:layout_marginTop="25sp"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addItem2"
        android:text="Add to cart"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/item2"
        app:layout_constraintBottom_toBottomOf="@id/item2"
        android:onClick="addItemToCart"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/item2"
        android:text="Item 3"
        android:layout_marginTop="25sp"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addItem3"
        android:text="Add to cart"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/item3"
        app:layout_constraintBottom_toBottomOf="@id/item3"
        android:onClick="addItemToCart"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
```
In the above code, we have added the onClick attribute and pass the addItemToCart method name.

And made the appropriate method in the second activity java.
```java
public void addItemToCart(View view) {
}
```
Now implement EventBus in the project. 

Let's make the subscriber and in the subscriber method, we will add items to the list.

See below to understand.

## Make Subscribe Method of EventBus to update Cart 

We will show cart count in the first activity so we need to update cart count in the subscriber method. Open your java file and create a method onCartItemAdd and in the parameter, we will get our CartEvent object.

to read further please go here: [EventBus Library for Android](https://www.akshayrana.in/2020/09/event-bus-in-android-with-example.html)

Thank you for reading have a nice day.