enter code hereI am new to android development. I am currently working on an app where it uses webview. In that webview I am passing an url and when the url loads that corresponding page has a button inside the webpage that has the functionality to exit the Webpage. My requirement is to get the click function of that button. How is it possible to implement it in android. If anyone knows please help
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
public class WebviewActivity extends Activity {
WebView web;
String rooId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent = getIntent();
rooId = intent.getStringExtra("RoomId");
Log.e("Roominer", "Roominer" + rooId);
web = (WebView) findViewById(R.id.webView1);
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("", "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i("", "Finished loading URL: " + url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.LENGTH_SHORT).show();
}
}
To link your Java code and Javascript code together, you can use something called a JavascriptInterface
This guide will give you a good idea to set this up.
https://developer.android.com/guide/webapps/webview.html
Don't forget to add the #JavascriptInterface annotation above the methods you want to use. This was implemented in SDK 17:
Caution: If you've set your targetSdkVersion to 17 or higher, you must add the #JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.
You can defined a button in your xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and set onClickListener at the button:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Put the action
}
});
Related
I'm trying to open up a Chrome custom tab from within a webview inside of an Android app. In order to do this, I figured I could register a custom URL handler(customtab://www.myurl.com/). The error I'm running into is that chromium(the webview) is blocking the custom URL handler due to an insecure content error (This request has been blocked; the content must be served over HTTPS.). The URL is https. Even adding webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); on the webView didn't work.
Any ideas on how I can register this custom handler as "secure"?
You can use web intent to open url in chrome
Intent webIntent = new Intent(Intent.ACTION_VIEW);
webIntent.setData(Uri.parse(url));
getActivity().startActivity(webIntent);
You need to override this method on the Webview to skip ssl errors
public synchronized void onReceivedSslError(WebView view, final SslErrorHandler handler,SslError error) {
handler.proceed();
}
I am open my custom URL in web view in android with follow below process.
Design your webview as full screen like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
android:gravity="center"
android:background="#ffffff">
<WebView
android:id="#+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"/>
</RelativeLayout>
And in you .java file add below code in onCreate method
webView = (WebView) findViewById(R.id.WebView);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
getWindow().setLayout(windowwidth, windowheight);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(false);
settings.setDomStorageEnabled(true);
try {
// here i call progressdialog which is load still the page is not load //properly
Constants.ShowProgressDialog(MyActivity.this, "", "Please wait...");
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("", "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i("", "Finished loading URL: " + url);
// dismiss the progressbar after load the web page in webview.
Constants.disMisProgressdialog();
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
});
webView.loadUrl("<your url>");
} catch (Exception e) {
e.printStackTrace();
}
If still any issue please provide me your url I'll check and provide you the code.
Hope your problme resolve.
Make sure provide permission in Manifest file.
"<"uses-permission android:name="android.permission.INTERNET" />" (remove the quatation mark)
You simply need to register a WebViewClient in your WebView and override the shouldOverrideUrlLoading() method to support your own scheme:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("custometab://")) {
// TODO: start custom tabs for the url.
return true;
} else {
return false;
}
}
(see https://stackoverflow.com/a/5514668/94363)
The problem is: how do you know when clicking on customtab://www.myurl.example.com/ if you should open http://www.myurl.example.com or https://www.myurl.example.com?
You could either introduce customtab:// and customtabs://
or do some manipulation of the URL customtab://http/www.myurl.example.com
But in fact, I don't think we need a custom scheme at all if you want to open all links with custom tabs. Just use:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO: start custom tabs for the url
return true;
}
Im facing one issue in Android 7.0 (moto g4) where im trying to load one url in the webview but it shows a blank white screen.It is working with Android M and below.
public class MainActivity extends Activity {
private String webviewURL="http://henmilholidayhomesgoa.com/player2/documentation/EULA.html";
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv= (WebView) findViewById(R.id.webview);
wv.getSettings().setTextZoom(60);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(webviewURL);
}
}
NOTE:- The url mentioned in the code is not the actual url which unfortunately i cant share :(.Its an url with https. But the given url in the post actually working ,Please let me know if any other input are needed.Thanks in advance
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</RelativeLayout>
Try this sample code:
In this there is a webview and a progress bar.In this code url will be loaded as soon as progress bar disappears and if there is any link within the url it will open it as well in the webview.
Below is the java code:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ShowWebView extends Activity {
//private Button button;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);
//Get webview
webView = (WebView) findViewById(R.id.webView1);
startWebView("http://www.androidexample.com/media/webview/login.html");
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(ShowWebView.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
// Open previous opened link from history on webview when back button pressed
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
Now below is the respective xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Please check this code in android 7.0. Hope it helps you out !
I have copied your same code and run the application on nougat Nexus 9 .
It take time to load the URL wait for 5 minutes otherwise check with other device of Nougat.
public class WebviewMainActivity extends AppCompatActivity {
private WebView wv;
private String webviewURL = "http://stackoverflow.com/questions/41425766/android-7-0-shows-blank-screen-when-trying-to-open-url-in-webview";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview_main);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setTextZoom(60);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(webviewURL);
}}
Hope this help.Happy coding.Please let me know if it was helpful.
I had similar issue on Asus Nexus 7. Webview version was 56 while Chrome browser app had lower version. After I updated Google Chrome app webview started working fine.
You can use loadDataWithBaseURL instead of loadData and try, I was facing similar issues on some devices.
We are trying to wrap a website within a mobile application. This website supports Chrome, IE9 and above and Safari. While trying to create an Android application to wrap the website, below is the layout which is created
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Here is my code
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
new GcmRegistrationAsyncTask(this).execute();
WebView myWebView = (WebView) findViewById(R.id.webview);
Intent intent = getIntent();
String url = intent.getStringExtra(Constants.URL);
myWebView.setWebChromeClient(new WebChromeClient());
//myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
If I just use WebViewClient, website works properly in full screen mode but as it is not compatible with Android Viewer, website is not seen properly. If i use WebChromeClient, it opens up the website properly in separate browser (Chrome) but shows URL Address bar. I went through lot of posts on the same but couldn't figure out the solution. Here is my end goal
I need to open the website in mobile application and i want to hide the address title bar. How can i achieve this?
I use the following and it works just like you want
WebView webView = (WebView) findViewById(R.id.webview);
ProgressBar progressBar = (ProgrssBar) findViewById(R.id.progressBar);
String url = "https://www.google.com";
progressBar.setIndeterminate(false);
progressBar.setMax(100);
WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.FAR;
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setDefaultZoom(zoomDensity);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.requestFocus(View.FOCUS_DOWN);
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(this.getWebViewClient());
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
webView.loadUrl(url);
And the WebChromeClient and WebViewClient implementations as;
WebChromeClient webChromeClient = new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
};
public WebViewClient getWebViewClient() {
return new WebViewClient() {
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.GONE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
//Some checks
if (url.contains("load.elsewhere.com")) {
//If you want to handle where load.elsewhere.com is loaded, say in another external browser
progressBar.setIndeterminate(true);
//startActivity to load elsewhere
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
};
}
The ProgressBar is something I add to show some progress
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</FrameLayout>
WebView does not have an address bar by default something else must be forwarding it to your browser. The API Guide for WebViews shows this example to override shouldOverrideUrlLoading:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
This will keep your app in your app and offsite links can be handled by the device's browser.
Here i am developing an android app as well as web page. I need to show the web content in my android activity without using browser.
I am using WebView for this requirement. But the problem is if i click the button it opens in browser.
Here i have to display that page in my android app as new activity.
This is the code i have tried for this issue.
webview.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:ignore="MergeRootFrame">
<WebView
android:id="#+id/activity_tab_host_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
this is my Admissionactivity which need to show the web content
public class AdmissionActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
webView = (WebView) findViewById(R.id.activity_tab_host_webview);
webView.loadUrl("http://www.google.com");
}
}
this is the code for the button click
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_college_listview);
intro = (TextView) findViewById(R.id.intro_button);
intro.setText("Introduction");
intro.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent admAct = new Intent(getApplicationContext(), AdmissionActivity.class);
// Clears History of Activity
admAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(admAct);
}
});
}
Just implement the web client and set it before loadUrl. The simplest way is:
myWebView.setWebViewClient(new WebViewClient());
//prevent web browser launch with loading http url
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});
Does PhoneGap (more specifically DroidGap for Android) have way to tell when its webview has been loaded. I'd like to find something like the following for PhoneGap:
Normal WebView Client Creation:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
Creating my PhoneGap Webview like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
if (isOnline() == true) {
WebSettings settings = mWebView.getSettings();
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
settings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://myurl.com");
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
}
});
}
else {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
Intent myIntent = new Intent(getContext(), LoadScreen.class);
startActivity(myIntent);
finish();
}
}
My Main_Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/splash"
/>
<WebView android:id="#+id/mWebView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
</RelativeLayout>
DroidGap does not have a method, since it's an activity, and not a view. Instead, the CordovaWebViewClient uses its onPageFinished method to send a message to all the plugins when the page has finished loading. This is what plugins can use to determine whether the page has finished. Conversely, you could check for the deviceready event in Javascript, which should be fired after the page has been loaded.
If you need to know when the WebView has loaded in Java for anything other than plugins, I highly recommend switching to using CordovaWebView and implementing the lifecycle events yourself, since this will give you more control over the augmented view. This also means that you have to make sure that plugins which use activities are handled correctly, otherwise they will break.