Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagexml
<com.mobilefuse.sdk.MobileFuseBannerAd
    app:mobilefusePlacementId="[INSERT PLACEMENT ID HERE]"
    app:mobilefuseAdSize="BANNER_320x50"
    
    android:id="@+id/bannerAdmf_banner_ad"
    android:layout_width="match_parent"
    android:layout_height="wrap_content50dp"

    app:mobilefuseAdSizelayout_constraintBottom_toBottomOf="BANNER_320x50parent"
    app:mobilefusePlacementId="<YOUR BANNER PLACEMENT ID>layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    />

Ensure that you replace <YOUR BANNER PLACEMENT ID> with a valid banner placement ID.

...

Code Block
languagejava
MobileFuseBannerAd banner = findViewById(R.id.bannerAdmf_banner_ad);
banner.loadAd();

Option 2: Create the Banner Ad programmatically

...

Example: Configure ad to refresh every 30 60 seconds:

Code Block
banner.setAutorefreshEnabled(true);
banner.setAutorefreshInterval(3060);

Example: Manually handle refreshing of the ad, for example you may want to fetch a new ad when a view is changed within your app:

Code Block
// On view change:
banner.forceRefresh();

Advanced: Set the ad background color

When the ad creative doesn’t fill the entire view area - for example when the view spans the entire width of the screen - the ad will sometimes be centred within the view. You can configure the background color of the view behind the ad to match the style of your app - or you can set it to transparent. The default behaviour is for the view to be transparent.

Setting the background in XML Layout:

Simply set the android:background attribute in the MobileFuseBannerAd tag in your Layout XML. The MobileFuse SDK exposes some useful colors to your project, but you can also use any theme or values color.

Code Block
languagexml
<com.mobilefuse.sdk.MobileFuseBannerAd
    //...
    android:background="@color/mobilefuse_transparent"
    />

Also available: mobilefuse_black, mobilefuse_white and mobilefuse_default_banner_bg

Setting the background programmatically:

You can also set the background color of the banner view programmatically:

Code Block
languagejava
// Set to a specific argb color:
bannerAd.setBackgroundColor(0xFFFF0000);

// Use a resource color:
bannerAd.setBackgroundResource(R.color.purple_200);

// Use a drawable resource/image:
bannerAd.setBackgroundResource(R.drawable.example_drawable_resource);

// Advanced - retrieve app theme color:
TypedValue resolvedThemeColor = new TypedValue();
getTheme().resolveAttribute(R.attr.colorSecondary, resolvedThemeColor, true);
bannerAd.setBackgroundResource(resolvedThemeColor.resourceId);

Example Code

Java

Expand
titleactivity_main.xml
Code Block
languagexml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.mobilefuse.sdk.MobileFuseBannerAd
        android:id="@+id/mf_banner_ad"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:mobilefuseAdSize="BANNER_320x50"
        app:mobilefusePlacementId="418310"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
Expand
titleMainActivity.java
Code Block
languagejava
import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.Toast;

import com.mobilefuse.sdk.MobileFuse;
import com.mobilefuse.sdk.MobileFuseBannerAd;
import com.mobilefuse.sdk.SdkInitListener;
import com.mobilefuse.sdk.privacy.MobileFusePrivacyPreferences;

public class MainActivity extends Activity {
    final int publisherId = 0;
    final int appId = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Set up the user's privacy preferences before init:
        MobileFuse.setPrivacyPreferences(
                new MobileFusePrivacyPreferences.Builder()
                        .setSubjectToCoppa(false)
                        .setUsPrivacyConsentString("1YNN")
                        .build()
        );
    
        Activity activity = this;
        MobileFuse.init(activity, publisherId, appId, new SdkInitListener() {
            @Override
            public void onInitSuccess() {
                Toast.makeText(activity, "SDK Init Success", Toast.LENGTH_SHORT).show();
    
                // Load our banner ad instance:
                MobileFuseBannerAd bannerAd = findViewById(R.id.mf_banner_ad);
                bannerAd.loadAd();
                
                // Set up auto-refresh each minute:
                bannerAd.setAutorefreshEnabled(true);
                bannerAd.setAutorefreshInterval(60);
                
                // Set our background color to match the app theme:
                TypedValue resolvedThemeColor = new TypedValue();
                getTheme().resolveAttribute(R.attr.colorPrimary, resolvedThemeColor, true);
                bannerAd.setBackgroundResource(resolvedThemeColor.resourceId);
            }
            
            @Override
            public void onInitError() {
                Toast.makeText(activity, "SDK Init Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Kotlin

Expand
titleactivity_main.xml
Code Block
languagexml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.mobilefuse.sdk.MobileFuseBannerAd
        android:id="@+id/mf_banner_ad"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:mobilefuseAdSize="BANNER_320x50"
        app:mobilefusePlacementId="418310"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
Expand
titleMainActivity.kt
Code Block
languagekotlin
import android.app.Activity
import android.os.Bundle
import android.util.TypedValue
import android.widget.Toast
import com.mobilefuse.sdk.*
import com.mobilefuse.sdk.privacy.MobileFusePrivacyPreferences

class MainActivity : Activity() {
    private val publisherId = 0
    private val appId = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Set up the user's privacy preferences before init:
        MobileFuse.setPrivacyPreferences(
            MobileFusePrivacyPreferences.Builder()
                .setSubjectToCoppa(false)
                .setUsPrivacyConsentString("1YNN")
                .build()
        )

        val activity: Activity = this
        MobileFuse.init(activity, publisherId, appId, object : SdkInitListener {
            override fun onInitSuccess() {
                Toast.makeText(activity, "SDK Init Success", Toast.LENGTH_SHORT).show()

                // Load our banner ad instance:
                val bannerAd: MobileFuseBannerAd = findViewById(R.id.mf_banner_ad)
                bannerAd.loadAd()

                // Set up auto-refresh each minute:
                bannerAd.autorefreshEnabled = true
                bannerAd.setAutorefreshInterval(60)

                // Set our background color to match the app theme:
                val resolvedThemeColor = TypedValue()
                theme.resolveAttribute(R.attr.colorPrimary, resolvedThemeColor, true)
                bannerAd.setBackgroundResource(resolvedThemeColor.resourceId)
            }

            override fun onInitError() {
                Toast.makeText(activity, "SDK Init Failed", Toast.LENGTH_SHORT).show()
            }
        })
    }
}