Android Sample Code

Unlock features, mobile partnerships & revenue with one drop-in SDK.

Sample Project

Note: For a full sample project head over to our button-android-samples repository.

Including the SDK

Using Gradle

You should change 2.0.0 to whatever is the latest version: Download

repositories {
    jcenter()
}

dependencies {
        // Add the Button SDK to your app
        compile 'com.usebutton:android-sdk:4.1.3'
        // Required for cross-app attribution, any version of the Google Play Services will work.
        compile 'com.google.android.gms:play-services-ads:8.3.0'
}

Manually

You can also Download the latest version and add it to your project's /libs folder.

Configure Button in the Application Manifest

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        // ...

        <meta-data android:name="com.usebutton.applicationid" android:value="YOUR-APP-ID"/>
    </application>

Note: If you don't know your Application ID, contact your Partner Success representative :)

Setup Button in Application onCreate

import com.usebutton.sdk.Button;

public class MyApplication extends Application {

// ...

  @Override
  public void onCreate() {
      super.onCreate();
      Button.getButton(this).start();
  }
}

Add a Button to Layout XML

The following code in your Activity will create a Button, add context and display it to your user.

<com.usebutton.sdk.ButtonDropin
        xmlns:button="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        button:btn_buttonId="YOUR-BUTTON-ID"
        />

Prepare for Display in Activity onCreate

public class MainActivity extends ActionBarActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    // Prepare the Button for display
    final ButtonDropin buttonDropin = (ButtonDropin)this.findViewById(R.id.button_dropin_id);

    // Let's assume we have a screen describing a location or point of interest
    final Location location = new Location("Button HQ", 40.7382965, -73.9823084);
    final ButtonContext context = ButtonContext.withSubjectLocation(location);

    // Let's also provide the user's location
    final android.location.Location userLocation = new LocationProvider(this).getBestLocation();
    if (userLocation != null) {
        context.setUserLocation(userLocation);
    }

    // Prepare the Button for display with our context
    buttonDropin.prepareForDisplay(context);
  }
}