Layar Developer Documentation

Back to layar.com

Tutorial- Example use of Android SDK Callbacks

As of version 8.0 the Layar SDK supports various callbacks. An overview of existing callbacks can be found in the documentation included in the SDK package.

You can now integrate the Layar SDK deeply into your own apps by making use of new advanced features:

In this  tutorial, we provide code example of how you can

Launch a geo Layar from a Campaign

If your SDK supports both vision and geo, you might want to launch a geo-layer from a Vision layer/ Campaign. If you do not handle the intent correctly, the native Layar app will be launched. In order to avoid this situation you need to use one of the available callbacks.

To follow this tutorial you need:

In order to implement the above scenario, we need to add a new class to our project that will include the callback method.

To add a new class in the project right click on the source project file and select New->class

In the window that pops-up, enter the name of the class we will call our class callback

This calls need to interact with the LayarSDKClient.We will use the callback method shouldTriggerAction callback method in order to define the behaviour. Our method should look like this:

	public boolean shouldTriggerAction(Layer layer, Poi poi, Action action) {
		
		Uri uri = Uri.parse(action.getUri());
		if ("layar".equals(uri.getScheme())) {
			needsResetScanMode = true;
			
			String layerName = uri.getAuthority();
			getSdkFragment().openLayer(layerName);
			return false;
		}
		
		return true;
	}

Since the geo-layer view will be launched directly from the AR view, when a user presses back we want him to go back to scan mode again. Therefore we will add a method onBackPressed :

public boolean onBackPressed() {
		if (needsResetScanMode && !getSdkFragment().isInScanMode()) {
			needsResetScanMode = false;		
			getSdkFragment().openScanMode();
			return true;
		}		

		return false;
}

Now our callback.java file looks like this:

Now we need to change our initial LaunchLayar.java file. We need to imprort our newly created class and modify the launch method. This should now look like this:

public void launch (View view) {	
		String oauthKey =  "yourkey"; 
		String oauthSecret =  "yoursecret"; 
		LayarSDK.initialize(this, oauthKey, oauthSecret);
		LayarSDK.startLayarActivity(this, new callback());;
	} 

The whole file should now look like this:

Save your project and run it. You should now be able to launch your geo-layer in your own app.

Add an automatic “Pop-out” functionality

 In this example we will show how you can change the default behaviour of the "Pop- out" feature. The callbacks we use below are supported by Layar SDK 8.1 and up. 

In our example the screen will pop out automatically once the user points away from the trigger image. In case the the image was not recognised the last tracked image will appear.

private static final String TAG = Callback.class.getSimpleName();
	@Override	
	public void onLayerLaunched(Layer layer, java.lang.String referenceImageId){

		 getSdkFragment().setDefaultPopOutUIEnabled(false); 
		
	 } 
	public void onReferenceImageLost(Layer layer, java.lang.String referenceImageId){ 
	getSdkFragment().triggerPopOut(referenceImageId); 	
	}

Hide the trigger image in the pop-out (8.4)

As of version 8.4 it is possible to choose whether to show to not the reference image when in Pop out mode. 

This is a simple boolean method and by default is set to true. To call this method you need to implement the SDKFragment class. 

getSdkFragment().setPopOutShowPreviewImage(false);

Re-attach the content back to the trigger image (8.4)

When a user is point away from the trigger image he is prompt to use the Pop Out mode. As of v8.4 it is possible to reattach the content back to the trigger image if the user points again back to it. 

To do that we need first to enable tracking while in Pop out mode. This can be done with the following callback:

getSdkFragment().setPopOutTrackingEnabled(true);

Then we need to reattach the content once it tracks the page. To do this wee need to create a method that once tracks the images, closed the Pop out view. This can be done as follows:

	public void onReferenceImageTracked(Layer layer, java.lang.String referenceImageId){

	    getSdkFragment().closePopOut() ;
	
	}