The Official Lyft Android SDK makes it easy to integrate Lyft into your app. More specifically, it provides:
repositories {
mavenCentral() // or jcenter()
}
dependencies {
compile 'com.lyft:lyft-android-sdk:2.0.2'
}
If you only want to use the Lyft API Wrapper or Deeplink portion of the SDK, you can pull them individually.
compile 'com.lyft:lyft-android-sdk-networking:2.0.2' // Lyft API Wrapper
compile 'com.lyft:lyft-android-sdk-deeplink:2.0.2' // Deeplink
<dependency>
<groupId>com.lyft</groupId>
<artifactId>lyft-android-sdk</artifactId>
<version>2.0.2</version>
<type>aar</type>
</dependency>
Adding the Lyft Button in an XML layout is as simple as:
<com.lyft.lyftbutton.LyftButton
android:id="@+id/lyft_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
lyft:lyftStyle="lyftMulberryDark"
/>
We recommend setting the width/height to wrap_content
, which results in a width of 260dp and a height of 50dp.
Otherwise, please keep in mind that a smaller width/height may result in undesirable UI, such as overlapping text.
To load ETA/cost:
ApiConfig apiConfig = new ApiConfig.Builder()
.setClientId("your_client_id")
.setClientToken("your_client_token")
.build();
LyftButton lyftButton = (LyftButton) findViewById(R.id.lyft_button);
lyftButton.setApiConfig(apiConfig);
RideParams.Builder rideParamsBuilder = new RideParams.Builder()
.setPickupLocation(37.7766048, -122.3943576)
.setDropoffLocation(37.759234, -122.4135125);
rideParamsBuilder.setRideTypeEnum(RideTypeEnum.STANDARD);
lyftButton.setRideParams(rideParamsBuilder.build());
lyftButton.load();
Addresses can also be used for pickup/dropoff locations. Each address results in an extra API call to obtain the corresponding lat/lng pair. Therefore, we recommend using lat/lng values directly if they are available.
RideParams.Builder rideParamsBuilder = new RideParams.Builder()
.setPickupAddress("185 Berry St, San Francisco, CA 94107")
.setDropoffAddress("2300 Harrison St, San Francisco, CA 94110");
Lyft is growing very quickly and is currently available in these cities. Please keep in mind that some ride types (such as Lyft Line) are not yet available in all Lyft cities. If you set the ride type of the button and it happens to be unavailable, the button will default to the Lyft Standard ride type. You can utilize the /v1/ridetypes
endpoint to get a list of the available ride types in an area.
To specify the button style via XML, use the lyft:lyftStyle
attribute or set it programmatically:
lyftButton.setStyle(LyftStyle.MULBERRY_DARK);
There are 5 styles to pick from:
The SDK provides wrapper methods around Lyft's REST APIs - this can be helpful when you want to build a more custom integration with the Lyft platform vs making HTTP requests directly.
The SDK uses Square's Retrofit 2 networking library. For each Lyft API endpoint, there is a corresponding Java method. The return type is a Call
object which can be executed synchronously or asynchronously. See LyftApi.java for all the API methods.
LyftApi lyftPublicApi = new LyftApiFactory(apiConfig).getLyftApi();
Call<EtaEstimateResponse> etaCall = lyftPublicApi.getEtas(37.7766048, -122.3943576, "lyft");
Asynchronous:
etaCall.enqueue(new Callback<EtaEstimateResponse>() {
@Override
public void onResponse(Call<EtaEstimateResponse> call, Response<EtaEstimateResponse> response) {
EtaEstimateResponse etaEstimateResponse = response.body();
Eta eta = etaEstimateResponse.eta_estimates.get(0);
}
@Override
public void onFailure(Call<EtaEstimateResponse> call, Throwable t) {
Log.d("MyApp", t.toString());
}
});
Synchronous:
EtaEstimateResponse etaEstimateResponse = etaCall.execute().body();
Eta eta = etaEstimateResponse.eta_estimates.get(0);
If you already use RxJava (your app must have it as a dependency), then you can obtain an Observable
instead of a Call
object. Simply use LyftApiRx instead of LyftApi.
LyftPublicApiRx lyftPublicApiRx = new LyftApiFactory(apiConfig).getLyftPublicApiRx();
Observable<EtaEstimateResponse> etaObservable = lyftPublicApiRx.getEtas(37.7766048, -122.3943576, "lyft");
If you are directly using the Lyft Wrapper API without using the Lyft Button
, then you may need to add the the ProGuard rules for Retrofit. Please see proguard-rules.pro. This only applies if you are using ProGuard.
The SDK provides direct deeplinking to the Lyft app for those developers who prefer to handle their own custom deeplinking vs relying on the Lyft Button.. The deeplink
module of the SDK includes this logic and makes it easy to launch the Lyft app.
DeepLinkParams deepLinkParams = new DeepLinkParams.Builder()
.setClientId("your_client_id")
.setRideType(RideTypeEnum.SHARED)
.setPickupLocation(37.7766048, -122.3943576)
.setDropoffLocation(37.759234, -122.4135125)
.build();
DeepLink.launchLyftApp(getContext(), deepLinkParams);
Checkout the sample activites which are included in the SDK, in the sample-app
module:
SampleBasicActivity
: Includes minimal code to set up the Lyft Button.SampleLocationAwareActivity
: Gets the device's current GPS location and calls the /v1/ridetypes
endpoint to get a list of the available ride types in the area (i.e. Lyft Line, Lyft Plus, etc). The user is able to select an available ride type via a dropdown. The Lyft Button then displays the current ETA and cost of that ride type to a specified destination.You can specify which activity to launch in the sample-app
's AndroidManifest.xml.
If you're looking for help configuring or using the SDK, or if you have general questions related to our APIs, the Lyft Developer Platform team provides support through our forum as well as on Stack Overflow (using the lyft-api
tag)
If you've found a vulnerability or a potential vulnerability in the Lyft Android SDK, please let us know at security@lyft.com. We'll send a confirmation email to acknowledge your report, and we'll send an additional email when we've identified the issue positively or negatively.