I am using the following code found on stackoverflow to make an app for my website.
It works, the website loads, but all links are loading inside the app.
I dont want links like facebook, youtube or any other external links to open inside my app.
How can I open external links outside my webview app ?
I am new to android coding, so if you can edit my code and post a full example answer, it will be great help.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://www.sitename.com");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
public class mywebClient extends WebViewClient{
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if((String.valueOf(request.getUrl())).contains("facebook")) {
view.loadUrl(String.valueOf(request.getUrl()));
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
}
return true;
}
}
Related
So I just implemented a simple webview application in which i was loading the stackoverflow main page. Earlier it was working just fine but now as I click on some link it opens that link in the default browser. I have implemented and override the shouldoverrideUrlLoading method by creating my custom webViewClient class.
I know that there are various question ask like these but I am writing this question only because they don't work for me.
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(".com"))
return false;
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
final customEditText editText = findViewById(R.id.urlEditText);
Button button = findViewById(R.id.enterButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("https://"+editText.getText().toString().trim().toLowerCase());
}
});
}
You can use this:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("url");
Just implement the web client and set it before loadUrl. The simplest way is:
WebView.setWebViewClient(new WebViewClient());
So When I was reading a tutorial https://www.journaldev.com/9333/android-webview-example-tutorial in this it is given that when shouldOverrideUrlLoading( ) method provides false then it url opens in our webview and if it returns true, then it will not load the page at all. So I think that earlier my code was working was because I was opening .com extensioned websites but when i open other extension website then it redirect to the default browser.
I just start android developing and I want to embed my website in the app.
When I want to open "http://www.google.com" , webpage opens in My app, but when I change address to my blog it wants to open it on external browser.
This is My activity code that I used to embed my site!
public class WebPage extends Activity {
#SuppressLint("SetJavaScriptEnabled") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_page);
//web view start
WebView med = (WebView) findViewById(R.id.webView1);
med.getSettings().setJavaScriptEnabled(true);
med.getSettings().
med.loadUrl("http://www.mediratour.com");
}
}
My webpage based on wordpress, I don't know if I have to change settings to prevent using external browser and opens it in My app.
Thanks
// Set this on your web view.
webView.setWebViewClient(new WebClient());
// Create this class.
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {
webView.loadUrl(url);
return true;
}
}
I think it helps.
Given the following code, the WebView will not navigate to and display google.com but instead a Choose Action Dialog will pop up with all installed browser apps to choose from.
Is this intended and is there a fix for this imo weird behaviour?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com/");
}
See Clicking URLs opens default browser
Essentially, you'll have to provide your own WebClient and override shouldOverrideUrlLoading
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Inside a webpage I try to open the following html and it works. As you can see it contains a list of videos. All this videos work perfectly when I open them inside my browser, but I fail to open them from my device. Practically they don't play!!!!
This is my webpage....each item is a link to a video...
After I click one of this items I get here, this is the video...but it just doesn't play.
IMPORTANT The video doesn't play even if I click on it, whatever I do...it just won't play.
And here is my code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
setContentView(R.layout.second);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new HelloWebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setPluginsEnabled(true);
myWebView.loadUrl("Here is set the link");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("Here is set the link")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setDataAndType(Uri.parse(url), "video/*");
view.getContext().startActivity(intent);
return true;
}
else{
return super.shouldOverrideUrlLoading(view, url);
}
}
}
IMPORTANT The videos work inside of a browser. Anyone can give me a solution in order for my videos to play? Thank you
Consider using a WebChromeClient instead.
private class HelloWebViewClient extends WebChromeClient {
//Code here
}
I have created a page which has link to a page of a website. So for showing that on I have used a WebView and it works fine.
My Problem is that when I click on any link given on that webpage, the link opens in phone's default browser view. But I want all the links to be opened in my created WebView.
Have I made any mistake or it is right..
Please Help Me
My code is as follows...
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.e("------------", ".........................................................................................");
setContentView(R.layout.terms_of_use_web_view_page);
btn_back = (Button) findViewById(R.id.terms_of_use_button_back);
btn_back.setOnClickListener(this);
webview = (WebView)findViewById(R.id.terms_of_use_webview);
webview.getSettings().setJavaScriptEnabled(false);
webview.loadUrl("http://www.oomphlink.com/terms-of-use/");
}
Try specifying your own WebViewClient:
WebView webView = (WebView)findViewById( R.id.terms_of_use_webview );
webView.setWebViewClient( new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url )
{
view.loadUrl( url );
return true;
}
});
To further understand why this is necessary have a look at the documentation for the shouldOverrideUrlLoading method.
wv.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient{
}
link for more info...