Created
April 14, 2017 22:52
-
-
Save ataliba/f0c2bd14cc590acbf078dcdbfc96f15c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.foo.app; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.view.KeyEvent; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.widget.ProgressBar; | |
public class MainActivity extends Activity { | |
private WebView mWebView; | |
ProgressBar progressBar; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mWebView = (WebView) findViewById(R.id.activity_main_webview); | |
progressBar = (ProgressBar) findViewById(R.id.progressBar1); | |
WebSettings webSettings = mWebView.getSettings(); | |
webSettings.setJavaScriptEnabled(true); | |
mWebView.loadUrl("http://www.foo.com"); | |
mWebView.setWebViewClient(new HelloWebViewClient()); | |
} | |
private class HelloWebViewClient extends WebViewClient{ | |
@Override | |
public void onPageStarted(WebView view, String url, Bitmap favicon) { | |
// TODO Auto-generated method stub | |
super.onPageStarted(view, url, favicon); | |
} | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView webView, String url) | |
{ | |
webView.loadUrl(url); | |
return true; | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
// TODO Auto-generated method stub | |
super.onPageFinished(view, url); | |
progressBar.setVisibility(view.GONE); | |
} | |
} | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) | |
{ //if back key is pressed | |
if((keyCode == KeyEvent.KEYCODE_BACK)&& mWebView.canGoBack()) | |
{ | |
mWebView.goBack(); | |
return true; | |
} | |
return super.onKeyDown(keyCode, event); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
public void onBackPressed() { | |
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( | |
MainActivity.this); | |
// set title | |
alertDialogBuilder.setTitle("Exit"); | |
// set dialog message | |
alertDialogBuilder | |
.setMessage("Do you really want to exit?") | |
.setCancelable(false) | |
.setPositiveButton("Yes",new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog,int id) { | |
// if this button is clicked, close | |
// current activity | |
MainActivity.this.finish(); | |
} | |
}) | |
.setNegativeButton("No",new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog,int id) { | |
// if this button is clicked, just close | |
// the dialog box and do nothing | |
dialog.cancel(); | |
} | |
}); | |
// create alert dialog | |
AlertDialog alertDialog = alertDialogBuilder.create(); | |
// show it | |
alertDialog.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment