How to set interstitial ads to be show for each n seconds ?


ScheduledExecutorService schedul = Executors.newSingleThreadScheduledExecutor();
schedul.scheduleAtFixedRate(new Runnable() {
    @Override    public void run() {
        Log.i("start interstitial","interstitial has been start");
        runOnUiThread(new Runnable() {
            @Override            public void run() {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Log.d("interstitial", "The interstitial wasn't loaded yet.");
                }

                loadInterstitialAd();
            }
        });

    }
},45,90,TimeUnit.SECONDS);
How to show dialog box that No internet connection in your android app

 @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isConnected()) {
            buildDialog(MainActivity.this).show();
        }
        else            {
                //what ever Your code is please put it here. 
            }

}

public boolean isConnected() {

    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = cm.getActiveNetworkInfo();

    if (netinfo != null && netinfo.isConnected())
    {
        android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if((mobile != null && mobile.isConnected()) || (wifi != null && wifi.isConnected()))
            return true;

         else return false;
    }
    else    return false;
}

public AlertDialog.Builder buildDialog(Context c) {

    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("No Internet Connection");
    builder.setMessage("please trun on wifi or Mobile Data to access the app and again run the app. Press ok to Exit");

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override        public void onClick(DialogInterface dialog, int which) {

            finish();
        }
    });

    return builder;
}
How to create a splash screen in android ?

In your first splash screen put the belwo code :

public class HomeActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT=3000;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        new Handler().postDelayed(new Runnable() {
            @Override            public void run() {
                Intent homeIntent = new Intent(HomeActivity.this,MainActivity.class);
                startActivity(homeIntent);
                finish();
            }
        },SPLASH_TIME_OUT);
    }
}


In the manifest file you have to make the below update : 


<application    android:allowBackup="true"    android:icon="@mipmap/ic_launcher"    android:label="@string/app_name"    android:roundIcon="@mipmap/ic_launcher"    android:supportsRtl="true"    android:theme="@style/AppTheme">

    <activity android:name=".HomeActivity" android:theme="@style/AppTheme.NoActionBar">

        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

<activity    android:name=".MainActivity"    android:label="@string/app_name"    android:theme="@style/AppTheme.NoActionBar">
   
</activity>
How to set toolbar margin in android . how to set the text in the center using toolbar margin in your activity .

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleMargin(25, 15, 35, 15);
}
How to show the adview in log android .


AdView mAdView;
mAdView.setAdListener(new AdListener() {
    @Override    public void onAdLoaded() {
        Log.i("AdEvent: ", "onAdLoaded");
    }

    @Override    public void onAdOpened() {
        Log.i("AdEvent: ", "onAdOpened");
    }

    @Override    public void onAdClosed() {
        Log.i("AdEvent: ", "onAdClosed");
    }
 @Override    public void onAdFailedToLoad(int errorCode) {
        String message = String.format("onAdFailedToLoad ", getError(errorCode));
        Log.d("AdEvent", message);
    }
});

//Method to return the error code 
private String getErrorReason( int errorCode){
    switch (errorCode) {
        case AdRequest.ERROR_CODE_INTERNAL_ERROR:
            return "Internal error";
        case AdRequest.ERROR_CODE_INVALID_REQUEST:
            return "Invalid request";
        case AdRequest.ERROR_CODE_NETWORK_ERROR:
            return "Network Error";
        case AdRequest.ERROR_CODE_NO_FILL:
            return "No fill";
        default:
            return "Unknown error";
    }
}

After you run the app you will find the log as below 


How to set a dialog when you click an item in menu items

How to set a dialog when you click an item in menu items

Java:


if(id==R.id.About)
{

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
 //Read UpdatealertDialog.setTitle("About");
alertDialog.setMessage("This App Includes information about Moccae Employees and Departments\n" + "Version: v 0.0");


alertDialog.setButton("Exit..", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
       
        dialog.cancel();
    }
});

alertDialog.show();}

XML:



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:app="http://schemas.android.com/apk/res-auto">

<item    android:id="@+id/About"  
  android:title="@string/About"  
  app:showAsAction="never" />


</menu>

How to open google play link app when you click over image in your app

How to open google play link app when you click over image in your app

Java:

ImageView SeniarImage = (ImageView)findViewById(R.id.SeniarImage);
SeniarImage.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=ae.gov.moccae.seniar&hl=en"));
        startActivity(intent);
    }
});

XML: 

<ImageView    android:id="@+id/SeniarImage"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_below="@+id/MoccaeAppImage"    android:layout_marginLeft="2dp"    android:layout_marginRight="2dp"    android:layout_marginBottom="2dp"    android:contentDescription="Seniar"    android:paddingRight="5dp"    android:paddingBottom="0dp"    android:src="@drawable/seniarimage" />

How to set  interstitial  ads to be show for each n seconds ? ScheduledExecutorService schedul = Executors. newSingleThreadScheduledExec...