Quick Start on
Android

Quick Start

In this quick start, we will create a simple Uber Button. The purpose of this quick start guide is for you, the developer, to understand Button and how it interacts within your application. In it, we'll walk through the steps needed to add a Button into your Android application.

Estimated time to complete: 20 minutes.

Table of contents

Step 1: Create a Button in the Dashboard

Create a new Uber Button in the Dashboard. During this process, you will:

  • Name your Button
  • Tell Button which platform your Button will be on (for this guide, it will be Android)
  • Tell Button which app your Button will be in (i.e. the name of your app)
  • Tell Button this will be an Uber Button


Come back to this guide after you have completed this.


Step 2: Allow JFRog bintray access

Note: when creating a new Android application, this step should already be completed, so there is no need to do this.

The easiest way to include the Button SDK into your Android application is by using Gradle. To do this, allow JFrog bintray access to your project's build.gradle file.

allprojects {
    repositories {
        jcenter()
    }
}

Step 3: Button SDK in app's build.gradle file

Add the Button SDK to your dependencies in your app's build.gradle file. Here is an example of what it will look like:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'

    // Button SDK
    compile 'com.usebutton:android-sdk:5.7.0' // I was added!
}

Step 4: AndroidManifest.xml file

Add the Button application ID as a meta-data element within the application element. Your application ID can be found in the Dashboard.

<application

    <activity
      <!-- your activities  -->
    </activity>

    <!--Button SDK-->
    <meta-data android:name="com.usebutton.applicationid" android:value="YOUR_BUTTON_APP_ID"/>
</application>

Step 5: Layout XML

The easiest way to add a Button is to add it to the layout of an Activity or Fragment in XML with the Button ID. Your Button ID can be found in the Dashboard.

<com.usebutton.sdk.ButtonDropin
        xmlns:button="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:id="@+id/main_dropin"
        android:layout_height="60dp"
        button:btn_buttonId="<#YOUR_BUTTON_ID#>"
        />

Step 6: MainActitivty.java file

Import the Button SDK at the top of the file.

import com.usebutton.sdk.Button;
import com.usebutton.sdk.ButtonContext;
import com.usebutton.sdk.ButtonDropin;
import com.usebutton.sdk.context.Location;
import com.usebutton.sdk.util.LocationProvider;

In your Application's onCreate method, configure Button. To dsiplay your Button, you'll need to provide it Context - this is how Buttons determine what to show. For an Uber Button, this is the name of the location, latitude, and longitude. Remember to replace these values below with a location close to you otherwise the Button may not render.

public class MyApplication extends Application {
  @Override
  public void onCreate() {
      super.onCreate();

      if (BuildConfig.DEBUG) {
        com.usebutton.sdk.Button.enableDebugLogging();
      }
      com.usebutton.sdk.Button.getButton(this).start();

      // Get the Dropin Button from layout XML file
      final ButtonDropin buttonDropin = (ButtonDropin)this.findViewById(R.id.main_dropin);

      // Set Button Context
      final Location location = new Location("Button HQ", 40.7382965, -73.9823084);
      final ButtonContext context = ButtonContext.withSubjectLocation(location);

      // Provide the user's location if we have the permission to
      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);
  }
}

Now you should have a simple Uber Button in your app!