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

 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 ) ; } }
In this tutorial, I will show how can I save String Android Internal Storage .txt file  Nested Class: 1.File file 2.FileOutputStream fileOutputStream 3.OutputStreamWriter outputStreamWriter Step 1: Android Manifest.xml Permission Add <? xml version ="1.0" encoding ="utf-8" ?> <manifest xmlns: android ="http://schemas.android.com/apk/res/android" package ="com.ruhul.string_save_txt_format" > <uses-permission android :name ="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission> <application android :allowBackup ="true" android :icon ="@mipmap/ic_launcher" android :label ="@string/app_name" android :roundIcon ="@mipmap/ic_launcher_round" android :supportsRtl ="true" android :theme ="@style/Theme.String_save_txt_format" > <activity android :name =".MainActivity" &
 phone authentication in android using Firebase Step 1:    Add Firebase to your Android project Step 2: Add this line build.Gradle(project) // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.1" classpath 'com.google.gms:google-services:4.3.5' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean( type : Delete) { delete rootProject . buildDir } Step 3: Add this dependency build.Gradle (app level) plugins { id 'com.android.application' } android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.ruhul.firebasephone