<?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.google.android.material.tabs.TabLayout android:id="@+id/tablayout_main" android:layout_width="match_parent" android:layout_height="70dp" android:layout_margin="0dp" android:background="@color/teal_200" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.google.android.material.tabs.TabItem android:id="@+id/Scan_Tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/scan_icon" android:text="Scan" /> <com.google.android.material.tabs.TabItem android:id="@+id/History_Tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/history_icon" android:text="History" /> <com.google.android.material.tabs.TabItem android:id="@+id/Setting_Tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/setting_icon" android:text="Settings" /> </com.google.android.material.tabs.TabLayout> <androidx.viewpager2.widget.ViewPager2 android:layout_margin="0dp" android:id="@+id/viewPager2" android:layout_width="match_parent" android:layout_height="488dp" app:layout_constraintBottom_toTopOf="@+id/Banner_adView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tablayout_main" app:layout_constraintVertical_bias="0.365" /> <com.google.android.gms.ads.AdView android:layout_margin="0dp" android:id="@+id/Banner_adView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" app:adSize="FULL_BANNER" app:adUnitId="@string/BANNER_AD_ID" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> </com.google.android.gms.ads.AdView> </androidx.constraintlayout.widget.ConstraintLayout>
// NOTE : Using the Fake AdUnit ID & APP ID for now. Change it when publishing to play store. public class MainActivity extends AppCompatActivity { private AdView mAdView; TabLayout tabLayout_main; ViewPager2 viewPager2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Google Admob Method MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); init(); setBannerAd(); } private void init(){ tabLayout_main = findViewById(R.id.tablayout_main); viewPager2 = findViewById(R.id.viewPager2); // setting adapter to viewpager2 pagerAdapter adapter = new pagerAdapter(this); viewPager2.setAdapter(adapter); // connecting tablayout tabs with viewpager2 new TabLayoutMediator(tabLayout_main, viewPager2, (tab, position) -> { String[] tab_names = {"SCAN","HISTORY","SETTING"}; tab.setText(tab_names[position]); }).attach(); } private void setBannerAd() { // Loading MainActivity BannerAd mAdView = findViewById(R.id.Banner_adView); AdRequest adRequest1 = new AdRequest.Builder().build(); mAdView.loadAd(adRequest1); // MainActivity BannerAd listener mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. super.onAdLoaded(); } @Override public void onAdFailedToLoad(LoadAdError adError) { // Code to be executed when an ad request fails. super.onAdFailedToLoad(adError); // Try again to load the ad mAdView.loadAd(adRequest1); } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. super.onAdOpened(); } @Override public void onAdClicked() { // Code to be executed when the user clicks on an ad. super.onAdClicked(); } @Override public void onAdClosed() { // Code to be executed when the user is about to return // to the app after tapping on an ad. super.onAdClosed(); } }); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)