Create and display inline ads
This tutorial will guide you through creating a simple application that can load and display a list of ads.
Section 1: Getting started with AdSDK
Add and configure the AdSDK to create and load ads.
Step 1
Let’s create a new Android project and remove all unnecessary code. Make sure you have added the correct packages from the readme.
Step 2
The entry point into the SDK is the AdService class. To make an easy start we just initialize the AdService in the onCreate
method of the MainActivity
. This is the first thing we should create, as it will be used to generate advertisements in the future. To do this, we will add this code:
coroutineScope.launch {
val isSuccess = AdService.init("1800", applicationContext, EventHandler())
Log.d("AdSDK", "Init is success: $isSuccess")
}
Lets create an App file and class to add the AdService initialisation. To do this, create this class and add the code snippet from above. The only mandatory parameter when creating AdService is the networkId. Network ID is the ID of your advertising account. Our Code should now look like this:
class App: Application() {
private val coroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
override fun onCreate() {
super.onCreate()
coroutineScope.launch {
val isSuccess = AdService.init("1800", applicationContext, EventHandler())
Log.d("App", "Init is success: $isSuccess")
}
}
}
Remember to add this class as an entry point, so add it to the application in your `Manifest:
android:name="com.adition.adsdk.App"
Step 3
The next step will be to create a composable, which we will display upon successful creation of AdService. On this composable, we will be creating and displaying our advertisement.
@Composable
fun AdView() {
Text(
text = "Advertisement should be here"
)
}
Now we can call this in our composable in the MainActivity
.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AdsdkdemoappandroidTheme {
AdView()
}
}
}
}
Section 2: Loading and displaying advertisements
Step 1
Before we create an Advertisement object we want to add a ViewModel
:
class MainViewModel: ViewModel() {
}
Step 2
To create an Advertisement object, you need to specify parameters, two of which are required:
- contentId or learningTag
- adTypes We’re going to use the contentId because it’s used more often than the learningTag. Content Unit is the unique ID of your advertising space and for ad type we use AdComposeRenderRegistry.getAllRendererNames() this will enable all available
adTypes
. Another important parameter isplacementType
. In this case, we need AdPlacementType.INLINE, which is the default, so we ignore it. All possible parameters can be found in the AdvertisementParameters documentation.
class MainViewModel: ViewModel() {
var ad: Advertisement = Advertisement(
"4810915",
AdComposeRenderRegistry.getAllRendererNames(),
)
}
We can now load this advertisement using loadAdvertisement:
class MainViewModel: ViewModel() {
var ad: Advertisement = Advertisement(
"4810915",
AdComposeRenderRegistry.getAllRendererNames(),
)
init {
viewModelScope.launch {
ad.loadAdvertisement()
}
}
}
Step 3
We can pass this Advertisement now to the rememberAdState composable. The rememberAdState creates the AdState which we will use later. There are multiple versions of the rememberAdState. For example we could pass a content unit directly to the rememberAdState and it would create the Advertisement for us.
@Composable
fun AdView(viewModel: MainViewModel) {
val adState = rememberAdState(advertisement = viewModel.ad)
}
Step 4
We can pass the AdState to Ad composable. The Ad is the main composable used to display the ad with provided adState
.
@Composable
fun AdView() {
val adState = rememberAdState(advertisement = viewModel.ad)
Ad(adState = adState, modifier = Modifier)
}
How our MainActivity should look like:
class MainActivity : ComponentActivity() {
private val viewModel by viewModels<MainViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AdsdkdemoappandroidTheme {
AdView(viewModel)
}
}
}
}
@Composable
fun AdView(viewModel: MainViewModel) {
val adState = rememberAdState(advertisement = viewModel.ad)
Ad(adState = adState, modifier = Modifier)
}
Now we should see the Banner on our device: