Created
May 15, 2016 22:44
-
-
Save stevesohcot/9e164de106b5a4e969c822fa74207716 to your computer and use it in GitHub Desktop.
Android Webview open URLs in new browser window
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
| // http://stevesohcot.com/tech-lessons-learned/2016/05/15/android-cannot-resolve-method-startactivityandroid-content-intent | |
| package com.diamondium.hashtagsaver; | |
| import android.app.Activity; | |
| import android.os.Bundle; | |
| import android.webkit.WebView; | |
| import android.webkit.WebViewClient; | |
| // Needed to open URLs in a new window | |
| import android.content.Intent; | |
| import android.net.Uri; | |
| public class MainActivity extends Activity { | |
| private WebView webview ; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| webview =(WebView)findViewById(R.id.webView); | |
| webview.setWebViewClient(new MyWebViewClient()); | |
| webview.getSettings().setJavaScriptEnabled(true); | |
| webview.getSettings().setDomStorageEnabled(true); | |
| webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER); | |
| webview.loadUrl("http://hashtagsaver.herokuapp.com/signup"); | |
| } | |
| } | |
| class MyWebViewClient extends WebViewClient { | |
| @Override | |
| public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
| if(url.contains("herokuapp.com")){ | |
| // load my page | |
| return false; | |
| } | |
| Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
| view.getContext().startActivity(intent); | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment