I have a webview. My question is, when user clicks to a link in webview, how to continue with my app?
When user clicks to a link, this dialog box appearing:
How can I avoid this dialogbox and continue with my app?
WebView Code:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(url);
Edit (MYActivity İn Test project)
package com.mycompany.myapp5;
import android.app.*;
import android.os.*;
import android.webkit.*;
public class MainActivity extends Activity
{
WebView view;
String url="http://google.com";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
}
This SO answer is exactly what you need.
The idea is to set a WebViewClient to your WebView and override shouldOverrideUrlLoading method like below:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
I hope this helps you!
Here is the complete code from a test project that is running.
The xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.test.myapplication.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
Code in the Activity:
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
String url="http://www.google.com";
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
This works fine in a 4.4 device. Please try this and let me know if you still have the issue.
Related
I'm loading one URL in WebView, the URL page contains functionality of capturing image from device camera OR select image from gallery. If I open URL in Browser, everything works fine but, if I open the same URL in WebView then Camera and Gallery functionality do not works. I think I'm missing some WebView setting. Please check my below code and help me to solve my problem.
Below is my WebViewActivity--
public class WebViewActivity extends AppCompatActivity
{
WebView myWebView;
ProgressBar myProgress;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_webview);
findViews();
}
private void findViews()
{
myWebView= (WebView) findViewById(R.id.myWebView);
myProgress= (ProgressBar) findViewById(R.id.myProgress);
myWebView.setWebViewClient(new myWebClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
//Other webview settings
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(false);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.loadUrl(SafeNestConstants.MEDLIFE_URL);
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
myProgress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
myProgress.setVisibility(View.GONE);
}
}
}
Below is my activity_my_webview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_terms_and_conditions"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypackage.WebViewActivity">
<WebView
android:id="#+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ProgressBar
android:id="#+id/myProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="#drawable/progress_medium"
android:visibility="visible" />
</RelativeLayout>
Also let me know if I can provide more information for the same. Thank you.
I have two activities in my android project. Both contents WebView and load same html(which include some javascript code also) file from assets folder.
When I load WebView in first activity it works fine. But after navigating to second activity, same html file doesn't load in WebView.
Activity 1:
public class Activity_1 extends AppCompatActivity {
WebView webView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__1);
webView = (WebView) findViewById(R.id.webview1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity_1.this, Activity_2.class);
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.loadUrl("file:///android_asset/sample.html");
}
#Override
protected void onPause() {
super.onPause();
}
}
Activity 2:
public class Activity_2 extends AppCompatActivity {
WebView webView;
ProgressDialog progressDialog ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
webView = (WebView) findViewById(R.id.webview2);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
}
#Override
protected void onResume() {
super.onResume();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
if (Build.VERSION.SDK_INT >= 19)
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
else
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressDialog.setProgress(newProgress);
}
});
webView.loadUrl("file:///android_asset/sample.html");
progressDialog.show();
}
}
activity_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next"
android:textSize="22sp"/>
</LinearLayout>
activity_2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
When I open second activity, I get stuck as,
I don't understand that same html is loading properly in first Activity, but not in the second.
I also tried with clear WebView cache in first activity and destroying WebView in onDestroy of activity 1.
To load html, I tried with following methods:
webView.loadUrl("file:///android_res/raw/sample.html");
OR
Access html from storing as string resource
webView.loadDataWithBaseURL(null, getResources().getString(R.string.html), "text/html", "UTF-8", null);
Edit:
I tried with uninstalling webview updates and its working fine.
Is there any problem with webview updates in android 5.0?
Please let me know what I am doing wrong?
I've done a similar project to try your code. It's working for me :S
Maybe you can try to place you file in src/main/assets and use this line
webView.loadUrl("file:///android_asset/sample.html");
But in my case is working with two options. ¿Can you provide more info about html file please?
This is my first question here. I know that this question has been asked before, but I didn't find an answer/solution that really explains the answer for a totally newbie like me.
I am creating an app with a linear layout that has a lot of buttons, each button should drive the user to a different web page. The buttons works well and every buttons goes to its specific web page, but in the default browser, not within the app.
This is my webview.xml file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
This is the WebViewActivity.java file:
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(
"http://egy-tech-droid.blogspot.com.eg/search/label/%D8%AA%D8%B7%D8%A8%D9%8A%D9%82%D8%A7%D8%AA%20%D8%AD%D8%B5%D8%B1%D9%8A%D8%A9");
}
I added the internet permission in the Manifest file:
<uses-permission android:name="android.permission.INTERNET" />
This opens the web page but in the default browser of the device and I want it to open inside my app. Any help? (please give me a detailed answer/explanation)
Add this to your code
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
You need to set up a WebViewClient in order to override that behavior (opening links using the web browser).
Use this;
webview.setWebViewClient(new WebViewClient());
Android documentation says:
public void setWebViewClient (WebViewClient client)
Sets the WebViewClient that will receive various notifications and
requests. This will replace the current handler.
Enjoy full code :
Oncreate () :
webView = (WebView) findViewById(R.id.webView1);
if(Constants.isNetworkAvailable(mContext)){
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() );
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.setInitialScale(30);
webView.loadUrl(url);
}else{
Toast.makeText(mContext, Constants.msgNoInternet, Toast.LENGTH_LONG).show();
}
MyWebViewClient :
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (!pDialog.isShowing()) {
pDialog.show();
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
//view.loadUrl(url);
System.out.println("on finish");
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
Kotlin version of Sunny's answer
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
view?.loadUrl(request?.url.toString())
return true
}
}
You can't talk about the Web nowadays without considering Javascript. By default, its use in a WebView is not active. To enable Javascript just insert these lines of code:
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
I hope this will help you.
In your web view layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#c7bbac">
<ImageView
android:id="#+id/txtmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/topbar50" />
<ImageView
android:id="#+id/backbutn"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:paddingTop="2dp"
android:src="#drawable/backbtn" />
</RelativeLayout>
<WebView
android:id="#+id/webView1"
android:layout_below="#+id/relay"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Webview Button Onclick:
webbutton = (ImageView) findViewById(R.id.web);
webbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
startActivity(intent);
}
});
Webview Activity:
public class WebViewActivity extends Activity {
private WebView webViewurl;
ImageView back;
AndroidInterface AMW = AndroidInterface.GetInstance();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
back = (ImageView) findViewById(R.id.backbutn);
webViewurl = (WebView) findViewById(R.id.webView1);
webViewurl.getSettings().setJavaScriptEnabled(true);
webViewurl.getSettings().setBuiltInZoomControls(true);
final Activity activity = this;
webViewurl.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
webViewurl.loadUrl("http://example.com");
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
webView = (WebView) findViewById(R.id.youtubelink);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("your url");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.co.in");
this code works fine...
Above code opens the link in your App.
Friends noticed that the webview my page loads but when you click the link it opens the navigated and I wanted to be loaded only within the webview, I would like min help?
I did the project below:
http://karthiktechfreak.blogspot.com.br/2015/07/profesional-android-webview-application.html
I'm not completely sure what your question is, but here is a Webview app that will work.
MainActivity
public class MainActivity extends Activity{
WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startWebView();
//Now, use webView.loadUrl(String url) whenever you want to load a website
}
void startWebView(){
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
activity_main.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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
I have 5 buttons and i need to open different webpages after buttons is clicked. How to make it?
my java code for webview activity:
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView v, String url)
{
v.loadUrl(url);
return true;
}
}
And xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="lv.shit.test.Sakums" >
<webview android:id="#+id/manswebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</webview>
</linearlayout>
What should I write on OnClick to open webpages in my webview?
You can load different pages on your webView object on the click.
as
webView.loadUrl("http://googlecom");
You can pas the url to the method loadUrl() under the webView object to preform the loading of different urls.
Call this method inside the onClick listener:
loadWebView(url);
public void loadWebView(String url){
/*
* Setting the options of my webView
*/
mWebView = (WebView)findViewById(R.id.manswebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mWebView.loadUrl(url);
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
#Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}
});
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
//when finish loading page
public void onPageFinished(WebView view, String url) {
}
});
//done loading now render the content
this.setContentView(mWebView);
}