import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.wb);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://surajbahadur.blogspot.in/");
//web chrome client used for loading
webView.setWebChromeClient(new WebChromeClient() {
final ProgressDialog pd = ProgressDialog.show(MainActivity.this,
"Loading", "Please Wait....");
@Override \
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
pd.dismiss();
}
super.onProgressChanged(view, newProgress);
}
});
}
@Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
exitDialog(MainActivity.this);
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private void exitDialog(Context ctx) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Exit");
builder.setMessage("Are you sure ?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
********************************************************************************
Code to make splash secreen
*******************************************************************************
package com.hitech.astrologeranilverma;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class SplashScreen extends AppCompatActivity implements Animation.AnimationListener {
Context ctx = SplashScreen.this;
public static boolean isOnline(Context c) {
boolean online = false;
TelephonyManager tm = (TelephonyManager) c.getSystemService(c.TELEPHONY_SERVICE);
if (tm != null) {
if (tm.getDataState() == TelephonyManager.DATA_CONNECTED) {
online = true;
} else {
ConnectivityManager connectivityManager = (ConnectivityManager)
c.getSystemService(c.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
online = info.isConnected();
}
}
}
}
return online;
}
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
StartAnimation();
}
private void StartAnimation() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative_layout);
rl.clearAnimation();
ImageView imageView = (ImageView) findViewById(R.id.astrologerIcon);
imageView.startAnimation(anim);
anim.setAnimationListener(this);
}
@Override public void onAnimationStart(Animation animation) {
}
@Override public void onAnimationEnd(Animation animation) {
if (isOnline(SplashScreen.this)) {
Intent intent = new Intent(ctx, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
SplashScreen.this.finish();
} else {
dialog(SplashScreen.this);
}
}
@Override public void onAnimationRepeat(Animation animation) {
}
private void dialog(SplashScreen splashScreen) {
runOnUiThread(new Runnable() {
@Override public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("No internet access");
builder.setMessage("Please connect to internet");
builder.setCancelable(false);
builder.setPositiveButton("Open Setting",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(Settings.ACTION_SETTINGS), 0);
}
});
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
No comments:
Post a Comment