Integration guide

ScrollView

Load SFWidget in XML

In your XML file, add the SFWidget to the bottom of your ScrollView:

<com.outbrain.OBSDK.SFWebView.SFWebViewWidget
  android:id="@+id/sf_widget"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  outbrain:ob_widget_id="MB_1"
  outbrain:ob_installation_key="NANOWDGT01" 
/>

You can specify the widgetID and the Installation key directly from the XML. Just add this attributes:

outbrain:ob_widget_id="YOUR-WIDGET-ID"
outbrain:ob_installation_key="YOUR-INSTALLATION-KEY" 

In your Activity:

public class YourActivity extends AppCompatActivity {

    private SFWebViewWidget mSFWebViewWidget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSFWebViewWidget = findViewById(R.id.sf_widget);

        final ScrollView scrollView = findViewById(R.id.scroll_view);

        mSFWebViewWidget.init(
            scrollView,
            "http://mobile-demo.outbrain.com" // Article URL
        );
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mSFWebViewWidget.onActivityConfigurationChanged();
    }
}

In order to provide your widgetID and Installation key from code, call to:

mSFWebViewWidget.init(
    scrollView,
    "http://mobile-demo.outbrain.com", // Article URL
    widgetID,
    installationKey
);

Load SFWidget programmatically

TODO, isn’t supported yet

RecyclerView

In your activity, initialize the RecyclerView and the mSFWebViewWidget, then pass it to your list adapter:

// use a linear layout manager
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(layoutManager);

mSFWebViewWidget = new SFWebViewWidget(
    recyclerView,
    "http://mobile-demo.outbrain.com", // Article URL
    "MB_1", // WidgetID
    "NANOWDGT01" // Installation key
);

ArticleListAdapter listAdapter = new ArticleListAdapter();
listAdapter.setSFWidget(mSFWebViewWidget);
recyclerView.setAdapter(listAdapter);

In your list adapter:

  • Item count:
@Override
public int getItemCount() {
    return ORIGINAL_RECYCLE_VIEW_SIZE + 1;
}
  • Item view type
@Override
public int getItemViewType(int position) {
    switch (position) {
        case 0:
            return ARTICLE_HEADER_VIEW_TYPE;
        case 1:
        case 2:
        case 3:
            return TEXT_VIEW_TYPE;
        default:
            return SF_WIDGET_VIEW_TYPE; // Last item should be the SFWidget
    }
}
  • onCreateViewHolder
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v;
    switch (viewType) {
        case TEXT_VIEW_TYPE:
            v = inflater.inflate(R.layout.article_text, parent, false);
            return new TextItemViewHolder(v);
        case ARTICLE_HEADER_VIEW_TYPE:
            v = inflater.inflate(R.layout.article_header, parent, false);
            return new ImageItemViewHolder(v);
        default:
            return new SFWebViewWidgetViewHolder(mSFWebViewWidget); // pass the SFWidget to SFWidgetViewHolder
    }
}

(Optional) Handle Click on Organic Recommendations Inside Your

Your activity should implement SFWebViewClickListener:

public class YourActivity extends AppCompatActivity implements SFWebViewClickListener {

Set the SFWebViewClickListener to SFWidget instance:

mSFWebViewWidget.setSfWebViewClickListener(this);

Then you can get clicks on organic recommendations:

@Override
public void onOrganicClick(String url) {
    // do something in your app
}

Support Orientation Change

In your Manifest file:

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize">
</activity>

In your activity, override onConfigurationChanged:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mSFWebViewWidget.onActivityConfigurationChanged();
}