Introduction

This is a simple tutorial that will help you integrate Pollfish surveys in your libGDX Android app.

Integration of Pollfish in an Android application is simple, and is described in detail in the official guide here: Pollfish Android Documentation

STEPS SUMMARY

  1. Sign Up as a Publisher at Pollfish website, create a new app and grab its API key from the dashboard
  2. Download Pollfish SDK (either Google Play or Universal) or reference it in your gradle file through jcenter()
  3. Add relevant Pollfish aar or jar file in your project, import relevant classes and add required permissions in your app’s manifest as described in the documentation
  4. Call Pollfish init function in your onResume of your AndroidLauncher
  1. package com.mygdx.game.android;
  2. import android.os.Bundle;
  3. import com.badlogic.gdx.backends.android.AndroidApplication;
  4. import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
  5. import com.mygdx.game.MyGdxGame;
  6. import com.pollfish.main.PollFish;
  7. import com.pollfish.constants.Position;
  8. public class AndroidLauncher extends AndroidApplication{
  9. @Override
  10. protected void onCreate (Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
  13. initialize(MyGdxGame(), config);
  14. }
  15. @Override
  16. public void onResume() {
  17. super.onResume();
  18. PollFish.initWith(this, new ParamsBuilder("YOUR_API_KEY").build());
  19. }
  20. }

With this simple implementation you should be able to see Pollfish surveys in your app within a few minutes.

Optional Steps

1. Listen to Pollfish listeners (optional)

You can listen to Pollfish listeners by implementing them in your AndroidLauncher, for example:

  1. import com.pollfish.interfaces.PollfishSurveyCompletedListener;
  1. public class AndroidLauncher extends AndroidApplication implements PollfishSurveyCompletedListener{
  1. @Override
  2. public void onPollfishSurveyCompleted(boolean playfulSurveys , int surveyPrice) {
  3. Log.d("Pollfish", "Pollfish survey completed - Playful survey: " + playfulSurveys + " with price: " + surveyPrice);
  4. }

2. Manually show or hide Pollfish (optional)

You can manually show or hide Pollfish in your Android App with a simple implementation as the following one:

In your and in your AndroidLauncher.java file:

  1. package com.mygdx.game.android;
  2. import android.os.Bundle;
  3. import com.badlogic.gdx.backends.android.AndroidApplication;
  4. import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
  5. import com.mygdx.game.MyGdxGame;
  6. import com.pollfish.main.PollFish;
  7. import com.pollfish.constants.Position;
  8. public class AndroidLauncher extends AndroidApplication implements MyGdxGame.MyPollfishCallbacks {
  9. protected MyGdxGame myGdxGame;
  10. @Override
  11. protected void onCreate (Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
  14. myGdxGame=new MyGdxGame();
  15. myGdxGame.setMyPollfishCallbacks(this);
  16. initialize(myGdxGame, config);
  17. }
  18. @Override
  19. public void onResume() {
  20. super.onResume();
  21. PollFish.initWith(this, new ParamsBuilder("YOUR_API_KEY")
  22. .customMode(true)
  23. .build());
  24. PollFish.hide();
  25. }
  26. @Override
  27. public void onShowPollfish() {
  28. PollFish.show();
  29. }
  30. @Override
  31. public void hidePollfish() {
  32. PollFish.hide();
  33. }
  34. }

and in your MyGdxGame.java file:

  1. package com.mygdx.game;
  2. import com.badlogic.gdx.ApplicationAdapter;
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.GL20;
  5. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  6. import com.badlogic.gdx.scenes.scene2d.InputEvent;
  7. import com.badlogic.gdx.scenes.scene2d.Stage;
  8. import com.badlogic.gdx.scenes.scene2d.ui.Skin;
  9. import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
  10. import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
  11. public class MyGdxGame extends ApplicationAdapter {
  12. private SpriteBatch batch;
  13. private Skin skin;
  14. private Stage stage;
  15. // Define an interface for your various callbacks to the android launcher
  16. public interface MyPollfishCallbacks {
  17. public void showPollfish();
  18. public void hidePollfish();
  19. }
  20. // Local variable to hold the callback implementation
  21. private MyPollfishCallbacks myPollfishCallbacks;
  22. // ** Additional **
  23. // Setter for the callback
  24. public void setMyPollfishCallbacks(MyPollfishCallbacks callback) {
  25. myPollfishCallbacks = callback;
  26. }
  27. @Override
  28. public void create() {
  29. batch = new SpriteBatch();
  30. skin = new Skin(Gdx.files.internal("data/uiskin.json"));
  31. stage = new Stage();
  32. final TextButton showBtn = new TextButton("Show", skin, "default");
  33. showBtn.setWidth(400f);
  34. showBtn.setHeight(100f);
  35. showBtn.setPosition(Gdx.graphics.getWidth() /2 - 600f, Gdx.graphics.getHeight()/2 - 10f);
  36. showBtn.addListener(new ClickListener(){
  37. @Override
  38. public void clicked(InputEvent event, float x, float y){
  39. if(myPollfishCallbacks!=null){
  40. myPollfishCallbacks.showPollfish();
  41. }
  42. }
  43. });
  44. final TextButton hideBtn = new TextButton("Hide", skin, "default");
  45. hideBtn.setWidth(400f);
  46. hideBtn.setHeight(100f);
  47. hideBtn.setPosition(Gdx.graphics.getWidth() /2 + 300f, Gdx.graphics.getHeight()/2 - 10f);
  48. hideBtn.addListener(new ClickListener(){
  49. @Override
  50. public void clicked(InputEvent event, float x, float y){
  51. if(myPollfishCallbacks!=null){
  52. myPollfishCallbacks.hidePollfish();
  53. }
  54. }
  55. });
  56. stage.addActor(showBtn);
  57. stage.addActor(hideBtn);
  58. Gdx.input.setInputProcessor(stage);
  59. }
  60. @Override
  61. public void dispose() {
  62. batch.dispose();
  63. }
  64. @Override
  65. public void render() {
  66. Gdx.gl.glClearColor(1, 0, 1, 1);
  67. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  68. batch.begin();
  69. stage.draw();
  70. batch.end();
  71. }
  72. @Override
  73. public void resize(int width, int height) {
  74. }
  75. @Override
  76. public void pause() {
  77. }
  78. @Override
  79. public void resume() {
  80. }
  81. }

3. Check if Pollfish survey is still available on your device

It happens that time had past since you initialized Pollfish and a survey is received. If you want to check if survey is still avaialble on your device and has not expired you can check by calling:

  1. PollFish.isPollfishPresent();