🤖 Android Integration

Android Integration

Integrating SnappGames into your Android application is straightforward. We provide two robust methods to launch your Unique Links: Chrome Custom Tabs (CCT) and a native WebView implementation.

Choosing the Right Method

Chrome Custom Tabs (CCT)

  • Best Performance: Uses the system browser engine.
  • Shared Cookies: Users remain logged into their Google/browser accounts.
  • Safe & Secure: Managed by Chrome for the latest security updates.
  • Recommended for Ad revenue: Better signal for ad providers.

Android WebView

  • Full UI Control: Customize the toolbar and navigation entirely.
  • In-App Experience: Keeps the user strictly within your app's UI.
  • Persistent Session: More control over how data is cached in-app.
  • Legacy Support: Helpful for apps with specific browser requirements.

Prerequisites

Ensure your app has the internet permission enabled in AndroidManifest.xml:

AndroidManifest.xmlxml
<uses-permission android:name="android.permission.INTERNET" />

1Update the build.gradle file

Add the following dependencies to your app/build.gradle file:

app/build.gradlegradle
dependencies {
  // Android WebView Support
    implementation("androidx.webkit:webkit:1.8.0")
}

2AOption 1: Chrome Custom Tabs (Recommended)

This method launches the game in a secure, high-performance browser tab within your app.

MainActivity.javajava
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();

findViewById(R.id.snappgamesButton).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Launch your SnappGames Unique Link
        String url = "https://www.snappgames.com/?id=44"; 
        customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));
    }
});

2BOption 2: Android WebView Integration

For a more customized experience, you can host the game directly within an Activity using a Managed WebView.

1. Create GameWebViewActivity

In Android Studio, go to app → src → main → java → your.package. Right-click and select New → Kotlin Class/File, and name it GameWebViewActivity.

2. Implementation details

GameWebViewActivity.ktkotlin
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.webkit.CookieManager
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity

class GameWebViewActivity : AppCompatActivity() {
    private lateinit var myWebView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game_webview)
        myWebView = findViewById(R.id.myWebView)

        configureWebViewSettings()

        // Retrieve game URL from Intent or use default
        val gameUrl = intent.getStringExtra("game_url") ?: "https://snappgames.com/?id=42"
        myWebView.loadUrl(gameUrl)
    }

    private fun configureWebViewSettings() {
        myWebView.settings.apply {
            javaScriptEnabled = true
            domStorageEnabled = true
            mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            setSupportZoom(false)
            builtInZoomControls = false
            displayZoomControls = false
        }

        myWebView.apply {
            setSoundEffectsEnabled(true)
            scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
            setBackgroundColor(Color.BLACK)
            webViewClient = WebViewClient()
        }

        // Ensure cookies are handled for session persistence
        CookieManager.getInstance().apply {
            setAcceptCookie(true)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                setAcceptThirdPartyCookies(myWebView, true)
            }
        }
    }
}

3Make the relevant XML changes

Ensure that the android:id attribute of your widget (Button or Image) is set to @+id/snappgamesButton.

res/layout/activity_main.xmlxml
<Button
    android:id="@+id/snappgamesButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play Game" />

Best Practices

Handling the Back Button (WebView)

When using a WebView, you should handle the device's back button to ensure users can navigate back through the game's history instead of immediately closing the Activity:

GameWebViewActivity.ktkotlin
override fun onBackPressed() {
    if (myWebView.canGoBack()) {
        myWebView.goBack()
    } else {
        super.onBackPressed()
    }
}

Frequently Asked Questions

What if Chrome is not installed on the user's device?

If Chrome is not present, Chrome Custom Tabs will automatically fall back to the default browser installed on the device, ensuring the game still opens.

Can I use Java for the WebView implementation?

Yes, the logic is identical. You can simply translate the Kotlin property and method calls to their Java equivalents.

Why is my WebView showing a blank screen?

Ensure you have added the INTERNET permission to your Manifest and that JavaScript is enabled in your WebView settings.