TOP > Banner Ads
- 1. Specify Ad Size
- 2. Define AdView in layout xml
- 3. Detect ads state
- 4. Avoid duplicates between multiple AdView
- 5. Load multiple ads at once
- 6. Use AdSpotCode
- 7. Set AdSpotBranchId
- 8. Use HardwareAccelerator
- 9. Test (Sample AdSpotId)
You need to specify AdSize
to adjust the size of the AdView
.
AdSize is enum class.
com.rakuten.android.ads.runa.AdSize
Size Type | Desciption |
---|---|
DEFAULT | The ad size set in DashBoard. |
ASPECT_FIT | Fit in display width. |
CUSTOM | Can be specified to any size. However, this specify is calculated based on the width. Specifiable range: (DEFAULT < CUSTOM < ASPECT_FIT) |
UNSAFE_CUSTOM | Can be specified to any size(pixel). This mode ignores the size of an ad content, please carefully use it. |
Set this AdSize to setAdViewSize of AdView.
Java
AdView adView = new AdView(context);
adView.setAdSpotId(123);
adView.setAdViewSize(AdSize.ASPECT_FIT);
R.layout.activity_main
<com.rakuten.android.ads.runa.AdView
android:id="@+id/adview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:adSpotId="@string/ad_spotid"
app:adSpotSize="AspectFit" />
app:adSpotId
: It is unique identifier what is issued per position where ad is to be displayed.※
layout_width
,layout_height
: The both parameters always sets "wrap_content
". AdView is sized automatically according to set adspot size on console.
Kotlin
MainActivity.kt
import com.rakuten.android.ads.runa.AdView
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<AdView>(R.id.adview).show()
}
...
Java
MainActivity.java
import com.rakuten.android.ads.runa.AdView;
...
@Overide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((AdView) findViewById(R.id.adview)).show();
}
...
To further customize the behavior of you ad, you can hook a number of events in the AdView's each one. You can catch for each events through an AdStateListener
class.
Below sample code is to use an AdStateListener
. Simply call the setAdStateListener
method of AdView.
AdStateListener
public abstract class
onLoadSuccess
: It is called this method, if it is loaded an advertise successfully.onLoadFailure
: It is called this method, in case of loaded an advertise failure. And if it failed, AdView will be hidden(View.INVISIBLE
) automatically.ErrorState
: Indicates the status when an error occurred in AdRequest.
onClick
: It is called this method, if it is clicked an advertise. (e.g. You use this method in case of to detect that clicked ad.)- You can get the click url from View which is the first argument of
onClick(adView: View?)
by casting to AdView.
- You can get the click url from View which is the first argument of
onLeftApplication
: This method is called afteronClick()
when a user click opens browser(transition to ad's destination URL).onAdClose()
: When a user returns to the app after viewing an ad's destination URL, this method is called.
Kotlin
import com.rakuten.android.ads.runa.AdStateListener
...
findViewById<AdView>(R.id.adview).apply {
adStateListener = object : AdStateListener() {
override fun onLoadSuccess() {
// Code to be executed when an ad finishes loading successfully
}
override fun onLoadFailure(adView: View?) {
// Code to be executed when an ad finishes loading failure.
adView?.let {
it.visibility = View.GONE
}
}
override fun onClick(adView: View?) {
// Code to be executed when clicked an ad.
// You can get the click url.
val clickUrl = (adView as AdView).clickUrl
}
}.show()
Java
import com.rakuten.android.ads.runa.AdStateListener;
...
AdView ad = (AdView) findViewById(R.id.adview);
ad.setAdStateListener(new AdStateListener() {
@Override
public void onLoadSuccess() {
// Code to be executed when an ad finishes loading successfully.
}
@Override
public void onLoadFailure(@Nullable View adView) {
// Code to be executed when an ad finishes loading failure.
if (adView != null) adView.setVisibility(View.GONE);
}
@Override
public void onClick(@Nullable View adView) {
// Code to be executed when clicked an ad.
// You can get the click url.
String clickUrl = (AdView)adView.clickUrl
}
});
ad.show();
Uses RunaAdSession
to avoid duplication of display ad content, in case of sets multiple AdView on same Screen.
Sets multiple AdView to the RunaAdSession$bind
method to avoid duplication of ads display in those AdViews.
And, when set multiple AdView in the bind
method, allow an interval to execute those show
method, because browse to the ads loaded of the previously bound AdView.
Below is just a sample to avoid duplication of content in AdView1 and AdView2. It's not necessarily required.
Kotlin
import com.rakuten.android.ads.runa.AdView
import com.rakuten.android.ads.runa.AdStateListener
...
private val adSession = RunaAdSession()
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adView1 = findViewById<AdView>(R.id.adview1)
val adView2 = findViewById<AdView>(R.id.adview2)
adSession.bind(adView1, adView2)
adView1.apply {
adStateListener = object: AdStateListener() {
override fun onLoadSuccess() {
adView2.show()
}
override fun onLoadFailure(adView: View?, errorState: ErrorState) {
adView2.show()
}
}
}.show()
}
...
※
show()
method of AdView2 must be call after load AdView1 completed.
Java
import com.rakuten.android.ads.runa.AdView;
import com.rakuten.android.ads.runa.AdStateListener;
...
private final RunaAdSession adSession = RunaAdSession();
...
@Override
fun onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
AdView adView1 = (AdView) findViewById(R.id.adview1);
AdView adView2 = (AdView) findViewById(R.id.adview2);
adSession.bind(adView1, adView2);
adView1.setAdStateListener(new AdStateListener() {
override fun onLoadSuccess() {
adView2.show();
}
override fun onLoadFailure(View adView) {
adView2.show();
}
});
}
adView1.show();
}
...
※ adView2 の show メソッドのコールは AdView1 の読み込み完了後に実行されるように実装する必要があります。
Use AdLoader
to display multiple ads with one load, such as displaying carousel ads.
AdLoader consists of a Builder pattern, so need to declare the AdLoader$Builder
class and add some parameters to load ads.
Add AdView to draw with the Builder, and sets AdLoaderStateListener
to detect the loading status of AdLoader as needed. Generates AdLoader with build
method after added parameters to the Builder, AdLoader starts loading ads that uses execute
method.
When loading starts, the loaded AdView will be drawn in sequence. At this time, the drawing order cannot be controlled.
Also, when loading ads with AdLoader, you don't need to implement deduplication with the previous item RunaAdSession
, because this includes deduplication functionality.
Kotlin
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adView1 = findViewById<AdView>(R.id.adview1).apply {
tag = "adview1"
}
val adView2 = findViewById<AdView>(R.id.adview2)
val adView3 = findViewById<AdView>(R.id.adview3)
val adLoader = AdLoader.Builder(view.context)
.add(adView1, adView2, adView3)
.with(object: AdLoaderStateListener() {
override fun onLoadSuccess(view: View?) {
}
override fun onLoadFailure(adView: View?, errorState: ErrorState) {
adView?.let { v ->
if (v.tag == "adview1") {
// Do something..
}
}
}
override fun onAllLoadsFinished(adLoader: AdLoader, loadedAdViews: List<AdView>?) {
// Do something
}
})
.build()
adLoader.execute()
}
...
※ This sample is assumed that the adSpotId setting for AdView is done on layout xml.
Java
@Override
fun onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState)
AdView adView1 = (AdView) findViewById(R.id.adview1);
adView1.setTag("adview1");
AdView adView2 = (AdView) findViewById(R.id.adview2);
AdView adView3 = (AdView) findViewById(R.id.adview3);
AdLoader adLoader = new AdLoader.Builder(view.context)
.add(adView1, adView2, adView3)
.with(new AdLoaderStateListener() {
@Override
public void onLoadSuccess(view: View?) {
}
@Override
public void onLoadFailure(adView: View?, errorState: ErrorState) {
adView?.let { v ->
if (v.tag == "adview1") {
// Do something..
}
}
}
@Override
public void onAllLoadsFinished(adLoader: AdLoader, loadedAdViews: List<AdView>?) {
// Do something
}
})
.build();
adLoader.execute();
}
...
※ This sample is assumed that the adSpotId setting for AdView is done on layout xml.
Kotlin
import com.rakuten.android.ads.runa.AdView
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<AdView>(R.id.adview).apply {
adSpotCode = "{AD_SPOT_CODE}"
}.show()
}
...
Java
import com.rakuten.android.ads.runa.AdView;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView) findViewById(R.id.adview);
adView.setAdSpotCode("{AD_SPOT_CODE}");
adView.show();
}
...
If you specify the same AdSpotID for multiple AdViews on the screen,
By specifying AdSpotBranchId, you can distinguish between them in the report screen of Runa.
Specify AdSpotBranch
for adSpotBranchId.
import com.rakuten.android.ads.runa.key.AdSpotBranch
binding.adView1.adSpotId = "111"
binding.adView1.AdSpotBranchId = AdSpotBranch.ID_1
binding.adView2.adSpotId = "111"
binding.adView2.AdSpotBranchId = AdSpotBranch.ID_2
binding.adView3.adSpotId = "111"
binding.adView3.AdSpotBranchId = AdSpotBranch.ID_3
Kotlin
import com.rakuten.android.ads.runa.AdView
import com.rakuten.android.ads.runa.key.Config
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<AdView>(R.id.adview).apply {
putProperty(Config.HardwareAcceleration.key, true)
}.show()
}
...
Sample display is possible with the following AdSpot ID.
Please make sure if it is implemented correctly.
Sample AdSpot ID | Size | Image |
---|---|---|
18261 | 300 x 250 | |
18262 | 320 x 50 | |
18263 | 320 x 100 | |
18264 | 160 x 600 | |
18265 | 728 x 90 | |
18266 | 336 x 280 | |
18267 | 970 x 90 | |
18268 | 970 x 250 | |
18269 | 300 x 600 |
LANGUAGE :