Skip to main content

AppBar ViewPager with  Tab layout in android 


XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainrelative_layout_id"
android:background="@color/colorPrimary"
android:theme="@style/Appthemnoactionbar"
android:layout_height="match_parent"
tools:context="com.ruhul.scanner.Fragment_Activity.ListFragment">



<com.google.android.material.appbar.AppBarLayout
android:id="@+id/tablayoutid"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="?actionBarSize">


<com.google.android.material.tabs.TabLayout
app:tabIndicatorColor="#0026FF"
app:tabSelectedTextColor="#000"
android:id="@+id/tab_id"
android:layout_width="match_parent"
android:layout_height="match_parent">

</com.google.android.material.tabs.TabLayout>

</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager

android:id="@+id/viewpagerid"
android:layout_below="@id/tablayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.viewpager.widget.ViewPager>

<LinearLayout
android:id="@+id/banner_container"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent" />

</RelativeLayout>


package com.ruhul.scanner.Fragment_Activity;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ruhul.scanner.R;
import com.ruhul.scanner.adapter.Tabaccesser_adapter;
import com.ruhul.scanner.tab_fragment.CreateFragment;
import com.ruhul.scanner.tab_fragment.ScancodehistoryFragment;
import com.facebook.ads.Ad;
import com.facebook.ads.AdError;
import com.facebook.ads.AdListener;
import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;
import com.facebook.ads.AudienceNetworkAds;
import com.facebook.ads.InterstitialAd;
import com.facebook.ads.InterstitialAdListener;
import com.google.android.material.tabs.TabLayout;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static android.content.ContentValues.TAG;


/**
* A simple {@link Fragment} subclass.
*/
public class ListFragment extends Fragment implements TabLayout.OnTabSelectedListener {

private View view;
private TabLayout tabLayout;
private ViewPager viewPager;

//banner
private AdView adView;

private Handler handler;


//interstitial add-------
private InterstitialAd interstitialAd;

public ListFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_list, container, false);
tabLayout=view.findViewById(R.id.tab_id);
viewPager=view.findViewById(R.id.viewpagerid);





handler = new Handler();



//interstial add initialize

interstitialAd = new InterstitialAd(getContext(), "763141957634732_771705420111719");//real interstial id





//banner add
adView = new AdView(getContext(), "763141957634732_771707073444887", AdSize.BANNER_HEIGHT_50);

// Find the Ad Container
LinearLayout adContainer = (LinearLayout) view.findViewById(R.id.banner_container);

// Add the ad view to your activity layout
adContainer.addView(adView);

// Request an ad
adView.loadAd();

AdListener adListener = new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
// Ad error callback
Toast.makeText(getContext(), "Error: " + adError.getErrorMessage(), Toast.LENGTH_LONG).show();
}

@Override
public void onAdLoaded(Ad ad) {
// Ad loaded callback
}

@Override
public void onAdClicked(Ad ad) {
// Ad clicked callback
}

@Override
public void onLoggingImpression(Ad ad) {
// Ad impression logged callback
}
};

// Request an ad
adView.loadAd(adView.buildLoadAdConfig().withAdListener(adListener).build());




Tabaccesser_adapter adapter = new Tabaccesser_adapter(getFragmentManager());
adapter.addFragment(new CreateFragment());
adapter.addFragment(new ScancodehistoryFragment());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(1);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

tabLayout.setupWithViewPager(viewPager);


// ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
//
// scheduler.scheduleWithFixedDelay(new Runnable() {
// public void run() {
//
//
//
// // Create listeners for the Interstitial Ad
// InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
// @Override
// public void onInterstitialDisplayed(Ad ad) {
// // Interstitial ad displayed callback
// Log.e("TAG", "Interstitial ad displayed.");
// }
//
// @Override
// public void onInterstitialDismissed(Ad ad) {
// // Interstitial dismissed callback
// Log.e(TAG, "Interstitial ad dismissed.");
// }
//
// @Override
// public void onError(Ad ad, AdError adError) {
// // Ad error callback
// Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
// }
//
// @Override
// public void onAdLoaded(Ad ad) {
// // Interstitial ad is loaded and ready to be displayed
// Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// // Show the ad
// interstitialAd.show();
// }
//
// @Override
// public void onAdClicked(Ad ad) {
// // Ad clicked callback
// Log.d(TAG, "Interstitial ad clicked!");
// }
//
// @Override
// public void onLoggingImpression(Ad ad) {
// // Ad impression logged callback
// Log.d(TAG, "Interstitial ad impression logged!");
// }
// };
//
// // For auto play video ads, it's recommended to load the ad
// // at least 30 seconds before it is shown
// if(interstitialAd.isAdInvalidated()) {
// return;
// }
// interstitialAd.loadAd(interstitialAd.buildLoadAdConfig().withAdListener(interstitialAdListener).build());
//
//
//
//
//
// }
//
//
//
//
// }, 1, 1, TimeUnit.MINUTES);

// Create listeners for the Interstitial Ad
InterstitialAdListener interstitialAdListener = new InterstitialAdListener() {
@Override
public void onInterstitialDisplayed(Ad ad) {
// Interstitial ad displayed callback
Log.e(TAG, "Interstitial ad displayed.");
}

@Override
public void onInterstitialDismissed(Ad ad) {
// Interstitial dismissed callback
Log.e(TAG, "Interstitial ad dismissed.");
}

@Override
public void onError(Ad ad, AdError adError) {
// Ad error callback
Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
}

@Override
public void onAdLoaded(Ad ad) {
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// Show the ad
//interstitialAd.show();

showAdWithDelay();



}

@Override
public void onAdClicked(Ad ad) {
// Ad clicked callback
Log.d(TAG, "Interstitial ad clicked!");
}

@Override
public void onLoggingImpression(Ad ad) {
// Ad impression logged callback
Log.d(TAG, "Interstitial ad impression logged!");
}
};


// For auto play video ads, it's recommended to load the ad
// at least 30 seconds before it is shown
interstitialAd.loadAd(interstitialAd.buildLoadAdConfig().withAdListener(interstitialAdListener).build());


return view;
}





private void showAdWithDelay() {
/**
* Here is an example for displaying the ad with delay;
* Please do not copy the Handler into your project
*/
// Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Check if interstitialAd has been loaded successfully
if(interstitialAd == null || !interstitialAd.isAdLoaded()) {
return;
}
// Check if ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
if(interstitialAd.isAdInvalidated()) {
return;
}
// Show the ad
interstitialAd.show();
}
}, 1000 * 60 * 4); // Show the ad after 15 minutes
}




@Override
public void onTabSelected(TabLayout.Tab tab) {

viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}


@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
if (interstitialAd != null) {
interstitialAd.destroy();
}
super.onDestroy();
}
}

Comments

Popular posts from this blog

 var val  deference in kotlin  fun main () { var Android_Programing_language : String = "java" println ( "android official programing language: " + Android_Programing_language ) Android_Programing_language = "Kotline" println ( "2019 google announced android official programing language: " + Android_Programing_language ) val Android_MultiPlatform_language : String = "Dart" println ( "multiple device run : " + Android_MultiPlatform_language ) /* Android_MultiPlatform_language="" /* note: var--------------valu changable note: val--------------valu not changable (final) */ */ }
 Firebase Cloud Messaging With  push notification with Image  public class ImageDownload extends AsyncTask < String , Void , Bitmap >{ @Override protected Bitmap doInBackground ( String ... strings ) { InputStream inputStream ; try { URL url = new URL( strings [ 0 ]) ; try { HttpURLConnection connection = ( HttpURLConnection ) url .openConnection() ; connection .connect() ; inputStream = connection .getInputStream() ; return BitmapFactory . decodeStream ( inputStream ) ; } catch ( IOException e ) { e .printStackTrace() ; } } catch ( MalformedURLException e ) { e .printStackTrace() ; } return null; } @Override protected void onPostExecute ( Bitmap bitmap ) { ShowNotification( bitmap ) ; } }
  Kotlin Function function means use big problems divided by submodule and easily problems solved.   It makes reusability of code and makes the program more manageable There are two types of functions: Standard library function User-defined function Standard Library Function Kotlin Standard library function is built-in library functions import java.time.LocalDateTime fun main (args: Array < String >) { val current = LocalDateTime.now() println( "Current Date and Time is: $current " ) } User-defined Function A user-defined is created by the user. User-defined function takes the parameter(s), perform an action and return the result of that action as a value. fun  main(args: Array<String>){       sum()       print( "code after sum" )   }   fun  sum(){       var num1 = 5        var...