# Android

The steps below will integrate ParOne into your Android app.

# Integrating ParOne

# Step 1

For Android apps, you may use a WebView and load our <script> tag and the desired <HTML> tag, as in the below example fragment.

private const val arg_id = "id"
private const val arg_feed = "feed"

class SingleBlockFragment : Fragment() {
  private var campaignID: Int = 0
  private var feed: String = "Unknown"

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
      campaignID = it.getInt(arg_id)
      feed = it.getString(arg_feed) ?: "Unknown"
    }
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val html = """
      <script src='parone.js'></script>
      <parone-video-block campaign='$this.campaignID' feed='$this.feed'/>
    """
    val webView = view.findViewById<WebView>(R.id.webview).apply {
      settings.javaScriptEnabled = true
      settings.domStorageEnabled = true
      webChromeClient = WebChromeClient()
      webViewClient = WebViewClient()
    }
    webView.loadDataWithBaseURL(
      "https://sdk.parone.io", html, 
      "text/html; charset=UTF-8", 
      "UTF-8", ""
    )
  }

  override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    return inflater.inflate(R.layout.fragment_single_block, container, false)
  }

  companion object {
    @JvmStatic
    fun newInstance(id: Int = 0, feed: String? = null) =
      SingleBlockFragment().apply {
        arguments = Bundle().apply {
            putInt(arg_id, id)
            putString(arg_feed, feed)
        }
      }
  }
}

DEVELOPMENT NOTE

Use env='stage' on the parone tags for testing. ParOne's integration team will tell you which environment to use when.

val html = """
<script src='parone.js'></script>
<parone-video-block 
  campaign='$this.campaignID' 
  feed='$this.feed' 
  env='stage'
/>
"""

# Additional Settings

See below for additional settings.

# Change Orientation

To support changing orientation, add the following to your AndroidManifest.xml in the activity tag:

android:configChanges="orientation|keyboardHidden|screenSize"

# Allowing Fullscreen

To support fullscreen, see example code below.

Fragment layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainview"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <WebView
            android:id="@+id/webview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </WebView>
    </FrameLayout>
    <FrameLayout
    android:id="@+id/targetview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">
    </FrameLayout>
</FrameLayout>

Extend the above Kotlin example like so:

private const val arg_id = "id"
private const val arg_feed = "feed"

class SingleBlockFragment : Fragment() {
  private var campaignID: Int = 0
  private var feed: String = "Unknown"
  private var targetView: FrameLayout? = null
  private var contentView: FrameLayout? = null
  private var customView: View? = null

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
      campaignID = it.getInt(arg_id)
      feed = it.getString(arg_feed) ?: "Unknown"
    }
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    contentView = view.findViewById(R.id.mainview);
    targetView = view.findViewById(R.id.targetview);
    val html = """
        <script src='parone.js'></script>
        <parone-video-block campaign='$this.campaignID' feed='$this.feed'/>
    """
    val webView = view.findViewById<WebView>(R.id.webview).apply {
      settings.javaScriptEnabled = true
      settings.domStorageEnabled = true
      webChromeClient = object: WebChromeClient() {
        override fun onShowCustomView(view: View, callback: CustomViewCallback) {
            targetView?.addView(view)
            customView = view
            contentView?.setVisibility(View.GONE)
            targetView?.setVisibility(View.VISIBLE)
            targetView?.bringToFront()
        }

        override fun onHideCustomView() {
          customView?.setVisibility(View.GONE)
          targetView?.removeView(customView)
          customView = null
          targetView?.setVisibility(View.GONE)
          contentView?.setVisibility(View.VISIBLE)
        }
      }
      webViewClient = WebViewClient()
    }
    webView.loadDataWithBaseURL(
      "https://sdk.parone.io", html, 
      "text/html; charset=UTF-8", 
      "UTF-8", ""
    )
  }

  override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    return inflater.inflate(R.layout.fragment_single_block, container, false)
  }

  companion object {
    @JvmStatic
    fun newInstance(id: Int = 0, feed: String? = null) =
      SingleBlockFragment().apply {
        arguments = Bundle().apply {
            putInt(arg_id, id)
            putString(arg_feed, feed)
        }
      }
  }
}
Last Updated: 10/10/2021, 3:19:20 PM