Created
June 3, 2019 00:05
-
-
Save ardovic/8cf9f58bb84e189d50f875d4a3258e6a to your computer and use it in GitHub Desktop.
Sample WebView implementation (Android Activity)
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
| import android.content.Intent; | |
| import android.graphics.Color; | |
| import android.net.Uri; | |
| import android.os.PersistableBundle; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import android.webkit.CookieManager; | |
| import android.webkit.WebChromeClient; | |
| import android.webkit.WebResourceRequest; | |
| import android.webkit.WebView; | |
| import android.webkit.WebViewClient; | |
| import android.widget.ProgressBar; | |
| public class SampleWebViewActivity extends AppCompatActivity { | |
| ProgressBar progressBar; | |
| WebView webView; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| progressBar = (ProgressBar) findViewById(R.id.progressBar); | |
| webView = (WebView) findViewById(R.id.webView); | |
| if (savedInstanceState != null) { | |
| webView.restoreState(savedInstanceState); | |
| } else { | |
| webView.getSettings().setJavaScriptEnabled(true); | |
| webView.getSettings().setSupportZoom(false); | |
| webView.getSettings().setBuiltInZoomControls(false); | |
| webView.getSettings().setLoadWithOverviewMode(true); | |
| webView.getSettings().setUseWideViewPort(true); | |
| webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); | |
| webView.setBackgroundColor(Color.WHITE); | |
| webView.setWebViewClient(new ourViewClient()); | |
| webView.setWebChromeClient(new WebChromeClient() { | |
| public void onProgressChanged(WebView view, int progress) { | |
| progressBar.setProgress(progress); | |
| if (progress < 100 && progressBar.getVisibility() == ProgressBar.GONE) { | |
| progressBar.setVisibility(ProgressBar.VISIBLE); | |
| } | |
| if (progress == 100) { | |
| progressBar.setVisibility(ProgressBar.GONE); | |
| } | |
| } | |
| }); | |
| String data = getIntent().getDataString(); | |
| if (Intent.ACTION_VIEW.equals(getIntent().getAction())) { | |
| webView.loadUrl(data); | |
| } else { | |
| webView.loadUrl("https://play.google.com/store/apps/details?id=com.serjardovic.angryfish.greenfish"); | |
| } | |
| } | |
| } | |
| public class ourViewClient extends WebViewClient { | |
| @Override | |
| public boolean shouldOverrideUrlLoading(WebView v, String url) { | |
| if(url.contains("com.serjardovic.angryfish.greenfish")){ | |
| v.loadUrl(url); | |
| CookieManager.getInstance().setAcceptCookie(true); | |
| } else { | |
| Uri uri = Uri.parse(url); | |
| startActivity(Intent.createChooser(new Intent(Intent.ACTION_VIEW, uri), "Choose browser")); | |
| } | |
| return true; | |
| } | |
| @Override | |
| public void onPageFinished(WebView view, String url) { | |
| super.onPageFinished(view, url); | |
| } | |
| } | |
| @Override | |
| public void onBackPressed() { | |
| if(webView.canGoBack()){ | |
| webView.goBack(); | |
| } else { | |
| super.onBackPressed(); | |
| } | |
| } | |
| @Override | |
| public void onSaveInstanceState(Bundle outState) { | |
| super.onSaveInstanceState(outState); | |
| webView.saveState(outState); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment