Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below I am trying to attach viewpager with the tablayout to display different fragments.I have set the tab icons in the xml. I do not know why they disappear during runtime & only the text appears in their place.

Please help me out here. Thank you.

activity_main.xml

    <?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>



MainActivity.java



// 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();
                }
            });
        }
    
    
    }


What I have tried:

I tried adding the tabs and setting the icons dynamically but that didnt work either
Posted
Comments
David Crow 5-Aug-21 8:54am    
Have you tried removing everything related to the banner ad to see what effect that has? If it has no effect, it can be removed from your post since it is irrelevant.

"I do not know why they disappear during runtime..."

Disappear implies that they are shown, even if only briefly. Do you mean, instead, that they are not showing at all? Are you sure they are supposed to? Some orientations and screen sizes do not show icons. Perhaps try a tablet-sized AVD to ensure it is not a screen "real estate" problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900