Last active
February 9, 2019 10:16
-
-
Save donrokzon/73016225906195e53e49b06411d9ce9a to your computer and use it in GitHub Desktop.
Webview
This file contains hidden or 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
public class WebActivity extends AppCompatActivity { | |
WebView myWebView; | |
public static String url="http://www.google.co.in"; | |
ProgressBar determinateBar; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_web); | |
Toolbar toolbar = findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
determinateBar=findViewById(R.id.determinateBar); | |
myWebView = (WebView) findViewById(R.id.webview); | |
myWebView.setWebViewClient(new MyBrowser()); | |
myWebView.getSettings().setLoadsImagesAutomatically(true); | |
myWebView.getSettings().setJavaScriptEnabled(true); | |
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); | |
myWebView.loadUrl(url); | |
} | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
// Check if the key event was the Back button and if there's history | |
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { | |
myWebView.goBack(); | |
return true; | |
} | |
// If it wasn't the Back key or there's no web page history, bubble up to the default | |
// system behavior (probably exit the activity) | |
return super.onKeyDown(keyCode, event); | |
} | |
private class MyBrowser extends WebViewClient { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
view.loadUrl(url); | |
return true; | |
} | |
@Override | |
public void onPageStarted(WebView view, String url, Bitmap favicon) { | |
super.onPageStarted(view, url, favicon); | |
determinateBar.setVisibility(View.VISIBLE); | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
super.onPageFinished(view, url); | |
determinateBar.setVisibility(View.INVISIBLE); | |
if (WebActivity.url.equals(url)) | |
view.loadUrl("javascript:" + | |
"var footer = document.getElementById(\"footer\"); " + | |
"footer.parentNode.removeChild(footer);" + | |
" var header = document.getElementsByTagName(\"header\")[0].style.display = 'none';"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment