I am implementing a webview. I want two buttons on the top of the web page.
I have one vertical linear layout, inside of which is one horizontal layout with two buttons, and one webview outside of the horizontal layout.
I am simply loading the Google URL in Java code.
Every time I run the application, it opens a new browser on top of the application and the buttons get hidden. It's not showing the buttons above the webview.
Please help and tell me how can I load a URL in the webview without opening another browser, or how I can prevent it by opening a native browser, so that the page is loaded in the webview itself and not a new browser.
Thanks all
Ya. You must implement WebViewClient class and Override shouldOverrideURLLoading() method in this class.
Why ? Because webview just open your "exactly link", if that link redirect other links, android will open default browser for this action.
In your example, as you know, when you connecting to google.com google will redirects to google at your country. Example, if you are in China, google will go to google.com.cn, if in Vietnam, will be google.com.vn.
Here is my simple example: (you can imagine this is an new browser, :laugh)
First is layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<EditText
android:id="#+id/url"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:hint="Input URL"/>
<Button
android:id="#+id/run"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0"
android:text="GO"/>
</LinearLayout>
<WebView
android:id="#+id/webview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
Here is code of main activity:
package com.basic;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class WebViewExample extends Activity{
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webview);
Button button = (Button) findViewById (R.id.run);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
gotoPage();
}
});
}
private void gotoPage(){
EditText text = (EditText) findViewById(R.id.url);
String url = text.getText().toString();
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webView.setWebViewClient(new Callback()); //HERE IS THE MAIN CHANGE
webView.loadUrl(url);
}
private class Callback extends WebViewClient{ //HERE IS THE MAIN CHANGE.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
}
Hope this help you :)
Adding the following code before loadUrl() will solve this problem,
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
The shouldOverrideUrlLoading() from WebViewClient does this job. Here goes the Android doc for shouldOverrideUrlLoading,
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url...
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29
My XML implementation:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:background="#android:color/transparent"
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
My Java implementation:
WebView webView;
public final String GlobalUrl = "http://slashdot.org/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_application_activity);
webView = (WebView) findViewById(R.id.webView);
loadWebViewLoad(webView);
}
private void loadWebViewLoad(WebView webview) {
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setSupportMultipleWindows(true);
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl(GlobalUrl);
}
And final result is:
The layout should something similar to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/button1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1"/>
<Button android:id="#+id/button2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1" />
</LinearLayout>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
And you can also refer to the Need help changing from stock android browser to webview to see if you are launching the url correctly.
I had the exact same problem and fortunately after browsing the web for about an hour I mixed some of the things I found and it worked.
this is the code:
WebView webView;
webView = ((WebView) rootView.findViewById(R.id.detail_area));
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(mItem.link);
where "detail_area" was my webview, rootView was my selected item inside a "Master/Detail Flow", link was the URL I wanted to open.
I tried this. its working for me. it does not open new window. it will open webview page only. its hiding for new browser asking window open..
private WebView webView;
String strUrl="url" ;
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(strUrl);
webView.setWebViewClient(new WebViewClient());
Add the below line in your xml file which having webview
tools:context=".MyActivity" (name of your activity)
This might be a late post but it might help other developers...
You need to set setWebViewClient on webview before loading the URL on it like below,
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
Some background of the above code, Documentation states in literals words like below about the shouldOverrideUrlLoading method.
* #param view The WebView that is initiating the callback.
* #param request Object containing the details of the request.
* #return {#code true} if the host application wants to leave the current WebView
* and handle the url itself, otherwise return {#code false}.
*/
#Override
#SuppressWarnings("deprecation") // for invoking the old shouldOverrideUrlLoading.
#RequiresApi(21)
public boolean shouldOverrideUrlLoading(#NonNull WebView view,
#NonNull WebResourceRequest request) {
if (Build.VERSION.SDK_INT < 21) return false;
return shouldOverrideUrlLoading(view, request.getUrl().toString());
}
If you see the documentation above for returning the value, it says,
#return {#code true} if the host application wants to leave the current WebView
*and handle the url itself, otherwise return {#code false}.
So it simply says, If you return true from shouldOverrideUrlLoading method, it'll ask the default browser of your device to handle the request of opening the URL and if you return false, then your URL will be loaded through webview only.
Now you can load your URL in webview either after this setWebViewClient call or you can also load your URL inside shouldOverrideUrlLoading method before returning the value.
this code work for me
thanks...
WebView wbView = (WebView) findViewById(R.id.webView);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.setWebViewClient(new WebViewClient());
wbView.loadUrl("http://ppid.polinela.ac.id");
Related
I have a webpage that uses Firebase auth to register/login users. I decided to convert the webpage into an app by using Android Studio's webView feature. I've been able to load the web url but when i try to log in, it doesn't work. I don't think firebase auth is working here.
I'm a web dev and not an app developer but I'm following some online tutorials for converting websites to apps using WebView in Android Studio.
Is there any thing I need to add in order to make firebase work in web apps?
Android Studio Code (MainActivity.java):
package com.example.indianrobostore;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
WebSettings webSettings;
private String webURL = "my website url";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.myWebView);
webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(webURL);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
}
XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myWebView"/>
</LinearLayout>
Any help would be appreciated. Thanks!
Okay, I went through a few articles and found I need to enable DOM Storage for this to work.
Code : webSettings.setDomStorageEnabled(true);
I have an app and i want to open the URL with Reading Mode
Without ads or something the reader don't need to see
How can i do that haw can i enable the reading mode on in kotlin ?
Try it with WebView,
In .xml file add this code:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
And inside activity(or fragment)
import android.os.Bundle;
import android.webkit.WebView;
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.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
I want to put the webversion of youtube in my application where you can do everything(like watch and search videos,..) without using a standard browser. I have written this code but every time pop ups which browser I want to use (like Google chrome,..)but I want to let it display on my appscreen and I have set the permission of internet already.
here the code:
youtube.xml:
<?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="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
youtube.java:
package com.example.listentomusic;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Youtube extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube);
WebView wv = (WebView)findViewById(R.id.webView1);
wv.loadUrl("http://www.youtube.com");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
You need to create your CustomWebViewClient & set it to your webView.
Like this -
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Now set this CustomeWebViewClient to your webView-
WV.setWebViewClient(new CustomWebViewClient());
Also main(String[] args) is of no use, remove it.
I am opening a simple flash based website in my webview using following code:
Java Code:
package com.sample.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class SampleWebViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webView = (WebView) findViewById(R.id.webview);
final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("loading");
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progress.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.dismiss();
}
});
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("http://www.fusioncharts.com/demos/features/#linkedcharts-for-easy-drill-down");
}
});
}
}
Main.xml:
<?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" >
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Load URL" />
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp" />
</LinearLayout>
Now when the page is loaded completely and i am trying to scroll down to view rest of the content, the webview is coming on top of "Load URL" button. Check the screenshot:
while it should look like:
Please suggest, what should i do so that it works correctly.
Edit: I am using a Galaxy Note(OSv2.3.5) with the latest version of both Flash Player and Adobe Air installed.
I have also tried this things but I think the only solution to do this is by using only a single webview in your xml, so, you don't have other native controls.
This is really broken on Android. If you try to use the Build in topbar it will also go behind the flash. I'd really recommend you go away (far away) from flash. There is some discussions about it on google groups and some on stackoverflow but all ends without any good results. The only good suggestion I found was using flash to cover other flash components =/
I want to open the following link in WebView
https://tickets.musiconelive.com/admin/SACValidateBarcode.asp
I am using following code to do that
web=(WebView)findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://tickets.musiconelive.com/admin/SACValidateBarcode.asp");
but it's not opening in WebView and instead is opening in the browser.
How can I fix this problem?
may this helps you
WebSettings mWebSettings;
WebView mWebView = (WebView)findViewById(R.id.services_detail_magnified_image);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
mWebView.setBackgroundColor(Color.TRANSPARENT);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
mWebView.loadUrl(StaticURL.uChangePassword);
mWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
//show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl (url);
return true;
}
}
i think this will help you.
package com.adySol;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class adySol extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="http://tickets.musiconelive.com/admin/SACValidateBarcode.asp";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(PluginState.ON);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(url);
}
}
Main.xml::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
>
<WebView android:id="#+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent"></WebView>
</LinearLayout>
Manifest permission :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
You need to set WebViewClient()
This is example in kotlin
webview.apply {
loadUrl("https://www.google.com/")
webViewClient = WebViewClient()
}
As per android documentation
public void setWebViewClient (WebViewClient client)
Sets the WebViewClient that will receive various notifications and requests. This will replace the current handler.
webView=findViewById(R.id.webView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR)
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(MyWebViewClient());
webView.loadUrl("https://google.com/");
class MyWebViewClient extends WebViewClient() {
#override
boolean shouldOverrideUrlLoading(WebView view , WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request)
}
}