Klarna Docs - Sign in with Klarna for Android

Sign in with Klarna for Android

Learn how to integrate Sign in with Klarna with your Android mobile app.

Set the return URL

You need to add the following snippet to your manifest file in order to set your return URL value for the SDK’s activity.

KOTLIN
<activity android:name="com.klarna.mobile.sdk.api.KlarnaCustomTabActivity" tools:node="replace" android:exported="true" android:launchMode="singleTop">
   <intent-filter>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="<YOUR-SCHEME>"/>
   </intent-filter>
</activity>

When you set the returnURL parameter for the KlarnaSignInButton or KlarnaSignInSDK classes, you need to concatenate the scheme and host into one value using :// as the separator, as shown in the snippet below:

KOTLIN
val button = KlarnaSignInButton(... returnURL =  “<YOUR-SCHEME>://<YOUR-HOST>”)

The returnURL value consists of the scheme and host separated with a ://.

Receive the results

While the user is interacting with the Sign in with Klarna flow or when the flow is completed, you'll receive events by setting an event handler when creating an instance of the SDK.

KOTLIN
val eventHandler = object : KlarnaEventHandler {
   override fun onEvent(klarnaComponent: KlarnaComponent, event: KlarnaProductEvent) {
      when(event.action) {
         KlarnaSignInEvent.USER_TAPPED_BUTTON -> {
            // User tapped the KlarnaSignInButton, auth process starting
         }
         KlarnaSignInEvent.USER_AUTH -> {
            // User completed interactive auth, tokens will be fetched
         }
         KlarnaSignInEvent.USER_CANCELLED -> {

To integrate the button for Sign in with Klarna as a native view in your Android application, you need to create an instance of the view, add it to your view hierarchy, and receive the results once the flow is completed.

The button for Sign in with Klarna native view in Android is called KlarnaSignInButton. You can create an instance programmatically or inflate it from an XML file. Additionally, you can also set parameters from the XML file.

Create the button programmatically

You can create the native view programmatically and place it in your application with desired layout options.

KOTLIN
//Create an instance of KlarnaSignInButton
val button = KlarnaSignInButton(...)

// Add it to your container with desired layout parameters
val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
containerViewGroup.addView(button, 0, layoutParams)

Create the button from XML

Optionally you can also create the native view in your XML file and get the view instance programmatically either by findViewById or by the generated DataBinding instance of your layout.

KOTLIN
 <com.klarna.mobile.sdk.api.signin.KlarnaSignInButton
      android:id="@+id/klarnaSignInButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      …
/>

Parameters used for the button are set as variables of the button instance or as attributes in the XML declaration.

ParameterTypeRequiredDescription
contextContextYesThe context of the Activity the button is in.
clientIdStringYes (optional in constructor)The UUID you get when creating your Klarna OAuth 2.0 app.
scopeStringYes (optional in constructor)A space-separated list of scopes you would like to request from the user. For example, request billing_address if you need the billing address in your account creation.The claims of the requested scopes will be returned as part of JWT token id_token.Note: openid is always requested by default even if you don't pass it.Available scopes:
  • offline_access
  • profile
  • email
  • phone
  • billing_address
  • national_identification_number
  • create_payment_session
marketStringYes (optional in constructor)The market or the country where this integration is available, for example, SE for Sweden.
localeStringYes (optional in constructor)The language in which the Sign in with Klarna button is presented to the user, for example, sv-SE or en-SE for Sweden.
eventHandlerKlarnaEventHandlerYes (optional in constructor)The interface to receive results from Klarna.
environmentKlarnaEnvironmentNoConfigures the endpoints and other behaviors that the SDK will be operating with.When set to production, the SDK will make requests to production endpoints and perform real validation, whereas for other environments will not.
regionKlarnaRegionNoDefines the regional API endpoints to which the SDK will send requests.
themeKlarnaThemeNoDefines the theming for the Sign in with Klarna UI, but not the button.
returnURLStringYes (optional in constructor)The URL schema used in your AndroidManifest.xml file when replacing our activity to return from the browser tab. Read more about redirect URLs.
loggingLevelKlarnaLoggingLevelNoConsole logging level for internal messages.
resourceEndpointKlarnaResourceEndpointNoDefines the cloud provider to which the SDK will send requests. This should not be changed or overridden.

There are also additional parameters that you can set to customize the look and feel of the Sign in with Klarna button.

ParameterTypeDescription
buttonThemeKlarnaButtonThemeEnumerated value to style the button in the light or dark theme.
buttonShapeKlarnaButtonShapeEnumerated value specifying the button's shape.
buttonLabelKlarnaButtonLabelEnumerated value specifying the label text that will be displayed on the button.

Klarna offers several theme, label, and shape options to customize the button's appearance to match the look and feel of your application.

The KlarnaButtonTheme enumeration defines which style is applied, depending on either the app’s configuration or the system setting.

NameDescription
KLARNARenders the button in the Klarna pink theme. We recommend this theme as it has the highest brand recognition and the likelihood of usage by your customers.
LIGHTRenders the button in white. We recommend this theme for dark backgrounds.
DARKRenders the button in black. We recommend this theme for light backgrounds.
AUTOAutomatic theme (light or dark) that will use the system’s user interface style.

In order for the button to fit into the aesthetic of your application, we offer different button shapes.

NameDescription
ROUNDED_RECTRenders the button as a rectangle with rounded corners.
PILLRenders the button as a pill-shaped button.
RECTANGLERenders the button as a rectangle with square corners.

To integrate the Sign in with Klarna SDK in your Android application, you need to create an instance of the SDK, call its signIn() function, and receive the results once the flow is completed.

Create the Sign in with Klarna SDK

The Sign in with Klarna SDK class in Android is called KlarnaSignInSDK. You need to create an instance programmatically and set the required parameters.

KOTLIN
//Create an instance of KlarnaSignInSDK
val klarnaSignInSDK = KlarnaSignInSDK(...)
ParameterTypeRequiredDescription
activityActivityYesThe activity instance of your app.
returnURLStringYesURL schema used in your AndroidManifest.xml file when replacing our activity to return from the browser tab. Read more about this here.
eventHandlerKlarnaEventHandlerYes (optional in constructor)The interface to receive results from Klarna.
environmentKlarnaEnvironmentNoEnumerated value to set the working environment for the SDK.
regionKlarnaRegionNoEnumerated value to set the geographical region of the user/application.
themeKlarnaThemeNoEnumerated value to specify how the content in the flow should be stylized on light and dark configurations.
loggingLevelKlarnaLoggingLevelNoConsole logging level for internal messages
resourceEndpointKlarnaResourceEndpointNoResource endpoint configuration for the SDK

To initiate the sign in flow for the user, you need to call the signIn() function from the SDK instance and specify the following parameters:

ParameterTypeRequiredDescription
clientIdStringYesThe UUID you get when creating your Klarna OAuth 2.0 app.
scopeStringYesA space-separated list of scopes you would like to request from the user. For example, request billing_address if you need the billing address in your account creation.The claims of the requested scopes will be returned as part of JWT token id_token.Note: openid is always requested by default even if you don't pass it.Available scopes:
  • offline_access
  • profile
  • email
  • phone
  • billing_address
  • national_identification_number
  • create_payment_session
marketStringYesThe market or the country where this integration is available, for example, SE for Sweden.
localeStringNoThe language to be presented to the user, for example, sv-SE or en-SE for Sweden.