---
title: "Using the Otto Event Bus library on Android"  
description: "Hi everyone in this article I’m explaining about Using the Otto Event Bus library on Android"  
author: "Manoj Pandey"  
published: 2015-03-16  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1663/using-the-otto-event-bus-library-on-android  
category: "android"  
tags: ["android", "android fragment", "android controls"]  
reading_time: 3 minutes  

---

# Using the Otto Event Bus library on Android

Otto is an event bus designed to decouple different parts of [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding) while

still allowing them to communicate efficiently.\

Forked from Guava, Otto adds unique [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) to an already refined event bus as well as specializing it to the Android platform.

Otto is a great way to communicate between [your activity](https://www.mindstick.com/interview/12746/the-last-callback-in-the-lifecycle-of-an-activity-is-ondestroy-the-system-calls-this-method-on-your-activity-as-the-final-signal-that-your-activity-instance-is-being-completely-removed-from-the-sys) and fragments or to communicate between an activity and a service.

##### Here is an example of Otto Event Bus library

1. Create an android project

2. The usage of this library in Java or Android is very simple, download the JAR and its one [requirement](https://yourviews.mindstick.com/view/85169/becoming-an-influencer-requirement-of-skills-and-knowledge) from Download Otto and [add it to your](https://answers.mindstick.com/qa/50569/how-to-create-second-account-and-add-it-to-your-iphone-8-instagram-app) classpath.

3. Add library in libs folder

4. My activity_main.[xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp)

```
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.vogella.android.otto.MainActivity"    tools:ignore="MergeRootFrame" />
```

5. Add new xml layout name as fragment_main.xml and add following views

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin" >     <Button        android:id="@+id/fragmentbutton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:text="Send event from fragment" /> </RelativeLayout>
```

6 . Paste following code in MainActivity.class

```
 package com.example.androidottoenevt; import android.os.Bundle;import android.app.Activity;import android.view.Menu; import android.app.Fragment;import android.view.LayoutInflater;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Toast; import com.squareup.otto.Bus;import com.squareup.otto.Produce;import com.squareup.otto.Subscribe;import com.squareup.otto.ThreadEnforcer; public class MainActivity extends Activity {                public static Bus bus;                 @Override                protected void onCreate(Bundle savedInstanceState) {                                super.onCreate(savedInstanceState);                                setContentView(R.layout.activity_main);                                if (savedInstanceState == null) {                                                getFragmentManager().beginTransaction()                                                                                .add(R.id.container, new PlaceholderFragment()).commit();                                }                                bus = new Bus(ThreadEnforcer.MAIN);                                bus.register(this);                }                @Subscribe                public void getMessage(String s) {                                Toast.makeText(this, s, Toast.LENGTH_LONG).show();                }                 @Override                public boolean onCreateOptionsMenu(Menu menu) {                                getMenuInflater().inflate(R.menu.main, menu);                                return true;                }                 @Override                public boolean onOptionsItemSelected(MenuItem item) {                                int id = item.getItemId();                                if (id == R.id.action_settings) {                                                TestData t = new TestData();                                                t.message = "Hello from the activity";                                                bus.post(t);                                                return true;                                }                                return super.onOptionsItemSelected(item);                }                 public class TestData {                                public String message;                }               /**                 * A placeholder fragment containing a simple view.                 */                 public static class PlaceholderFragment extends Fragment {                                 public PlaceholderFragment() {                                }                                 @Override                                public View onCreateView(LayoutInflater inflater, ViewGroup container,                                                                Bundle savedInstanceState) {                                                View rootView = inflater.inflate(R.layout.fragment_main, container,                                                                                false);                                                View button = rootView.findViewById(R.id.fragmentbutton);                                                button.setOnClickListener(new View.OnClickListener() {                                                                 @Override                                                                public void onClick(View v) {                                                                                bus.post("Hello from the Fragment");                                                                }                                                });                                                bus.register(this);                                                return rootView;                                }                                 @Subscribe                                public void getMessage(MainActivity.TestData data) {                                                Toast.makeText(getActivity(), data.message, Toast.LENGTH_LONG)  .show();                                }                }                 @Produce                public String produceEvent() {                                return "Starting up";                } }
```

7. My Manifest.xml file

```
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.androidottoenevt"    android:versionCode="1"    android:versionName="1.0" >     <uses-sdk        android:minSdkVersion="10"        android:targetSdkVersion="18" />     <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.androidottoenevt.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />           <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application> </manifest>
```

Now run your application

![Using the Otto Event Bus library on Android](https://www.mindstick.com/mindstickarticle/9c7a4a2a-ccd6-4528-917b-43ec62b0629d/images/d9d21448-e801-446e-8f91-6981c5020de6.png)

---

Original Source: https://www.mindstick.com/articles/1663/using-the-otto-event-bus-library-on-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
