How to create a Custom MoPub GDPR Consent Dialog in Android using Kotlin

To create a MoPub account, follow the tutorial: How to Create an Account on MoPub

In this tutorial, I will show you how to create a custom GDPR consent dialog for MoPub using Kotlin by adding our own Activity with text and colors.

MoPub offers its own consent dialog to use when you set up their SDK in your app.

This gives us very limited customization, and in most cases, the colors and the feeling don’t fit in our app design and make users suspicious and rate our app negatively, or delete our app completely from their device.

In the image above you see the number of active installs drops after the GDPR Consent dialog (src. Reddit)

So, to fix that, it’s good to make our GPDR consent dialog as friendly as possible and make the users understand what’s going on in this situation.

Suppose we have MoPub already installed in our project, so let’s make our own dialog.

If you don’t have MoPub on your Android app yet, check out the tutorial: How to add MoPub Ads in your Android app using Kotlin

Adding Custom GDPR Consent Dialog

First, let’s create our Activity by right-clicking on our package name folder and go to New>Activity>Empty Activity

..and give it the name CustomConsentDialog and press Finish.

Next, go to the activity_custom_consent_dialog.xml file under the layout folder and paste:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" xmlns:app="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" tools:context=".CustomConsentDialog"> <TextView android:id="@+id/gdpr_main_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." android:textColor="@android:color/white" android:textSize="16sp" android:textStyle="bold" app:lineHeight="20sp" /> <TextView android:id="@+id/gdpr_note_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/gdpr_main_text" android:layout_margin="10dp" android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam" android:textColor="@android:color/white" android:textSize="16sp" android:textStyle="bold" app:lineHeight="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_marginStart="15dp" android:layout_marginLeft="15dp" android:layout_marginEnd="15dp" android:layout_marginRight="15dp" android:layout_marginBottom="55dp" android:orientation="horizontal" android:weightSum="2"> <Button android:id="@+id/gdpr_agree_btn" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginEnd="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@color/colorPrimaryDark" android:text="Yes, I agree." android:textAllCaps="false" android:textColor="@android:color/white" android:textStyle="bold" /> <Button android:id="@+id/gdpr_disagree_btn" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginStart="5dp" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@color/colorPrimaryDark" android:text="No thank you." android:textAllCaps="false" android:textColor="@android:color/white" android:textStyle="bold" /> </LinearLayout> <TextView android:id="@+id/gdpr_footer_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginStart="15dp" android:layout_marginLeft="15dp" android:layout_marginEnd="15dp" android:layout_marginRight="15dp" android:layout_marginBottom="12dp" android:lines="2" android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempo" android:textAlignment="center" android:textColor="@android:color/white" android:textStyle="bold" /> </RelativeLayout>
Code language: HTML, XML (xml)

Now go to the CustomConsentDialog.kt file and paste:

class CustomConsentDialog : AppCompatActivity() { private lateinit var mPersonalInfoManager: PersonalInfoManager override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_custom_consent_dialog) title = "Personalize Your Experience" mPersonalInfoManager = MoPub.getPersonalInformationManager()!! gdpr_main_text.text = Constant.MoPub.MAIN_TEXT gdpr_note_text.text = Constant.MoPub.NOTE_TEXT gdpr_footer_text.text = Constant.MoPub.FOOTER_TEXT gdpr_agree_btn.setOnClickListener { // Call this to let MoPub know the user has granted consent mPersonalInfoManager.grantConsent() finish() } gdpr_disagree_btn.setOnClickListener { // Call this to let MoPub know the user has revoked consent. mPersonalInfoManager.revokeConsent() finish() } } }
Code language: Kotlin (kotlin)

Adding the Text

In this example, I created a Kotlin class named Constant, so we can organize the texts for our consent dialog:

class Constant { class MoPub { companion object { val BANNER_AD_UNIT = "b195f8dd8ded45fe847ad89ed1d016da" val MAIN_TEXT = "This app personalizes your advertising experience " + "through MoPub, a division of Twitter International " + "company. By consenting to this enhanced ad " + "experience, you'll see ads that MoPub and it's " + "partners believe are more relevant to you. " + "Depending on your privacy settings, MoPub and it's " + "partners may collect and process personal data " + "such as device identifiers, location data, and other " + "demographic and interest data about you to " + "provide this personalized advertising experience." val NOTE_TEXT = "By agreeing, you are confirming that you are over " + "the age of 16 and would like a personalized ad " + "experience." val FOOTER_TEXT = "I understand that I will see ads, but they may " + "not be as relevant to my interests" } } }
Code language: Kotlin (kotlin)

Of course, you can change it in your text, that’s why we created a Custom GDPR Consent Dialog.

Last step…

Now just go to your file where MoPub was calling to show the default dialog before, in our example MyMoPub.kt, and add our Activity we created(CustomConsentDialog):

private fun GDPRConsent() { mPersonalInfoManager = MoPub.getPersonalInformationManager()!! Log.d("MoPub", "This country covered by GDPR? ${mPersonalInfoManager.gdprApplies()}") if (mPersonalInfoManager.shouldShowConsentDialog()) { val intent = Intent(mContext, mContext.startActivity(intent) }
Code language: Kotlin (kotlin)

..and run it!

You can find the final project here

If you have any questionsplease feel free to leave a comment below

Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments

Get once a week my latest tutorials right in your inbox

Check your inbox to confirm your email