Android Webview doen't work correctly - android

This site works well in android browsers but it doesn't work in android webview.
I set internet permission and did all things.
Who can teach me?
public class MainActivity extends AppCompatActivity {
WebView web_view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this).init();
setContentView(R.layout.activity_main);
web_view = (WebView)findViewById(R.id.webView);
getSupportActionBar().hide();
web_view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
web_view.getSettings().setDomStorageEnabled(true);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebViewClient(mWebViewClient);
web_view.addJavascriptInterface(new MyJavaScriptInterface(), "android");
web_view.loadUrl("http://182.72.55.182/pms/employee");
}
WebViewClient mWebViewClient = new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");
};
};
class MyJavaScriptInterface {
#JavascriptInterface
public void onUrlChange(String url) {
Log.d("hydrated", "onUrlChange" + url);
}
}
}

make sure that your regarding website becoming live in everywhere!
webview doesn't display any local host website

Try to put WebViewClient part inside the onCreate.

Related

Open new tabs on default browser when using webview

I want to tell you I never try android development before, and this is the first time I'm doing an android app I build a website using ASP.NET and I want to run that site within the android app web view.
So followed a few tutorials and I did so far.
Here when I redirect to some external sites In my site, I open them on another new tab on the browser, here I want to open it on the mobile phone's default browser instead of the opening inside the web view.
need a help to doing that.
This is my app MainActivity code.
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.mycustom:5500/test/");
WebSettings websettings = webView.getSettings();
websettings.setJavaScriptEnabled(true);
}
private class MyWebView extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
}
#Override
public void onBackPressed() {
if (webView.isFocused() && webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
}

How can I block ad/popup/domain

How can I block ad/popup/domain
Hello, I'm making a simple webview and I would like that in this webview I could limit which domains my user can access, for example, I would like only google.com and youtube.com to be accessed, how can I do this?
Is this the best way to block popup and advertisement?
public class MainActivity extends AppCompatActivity {
WebView wv;
public void onBackPressed(){
if(wv.canGoBack()){
wv.goBack();
}else{
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView);
//Enable Javascript
wv.getSettings().setJavaScriptEnabled(true);
wv.setFocusableInTouchMode(true);
// set Render Priority to high
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// Load url
wv.loadUrl("https://www.google.com/");
wv.setWebViewClient(new myWebViewClient());
}
String currentUrl;
private class myWebViewClient extends WebViewClient{
#Override
public void onLoadResource(WebView view, String url) {
Log.e("Resource ",url);
}
}
}
Before calling loadUrl, just perform checks on the String. If it is anything other than you want, display something like a snackbar with the message

How to get url after loading page in webview

I need to take url after loading a web page in my web view to check condition and if I use onPageFinished() method, then where to use.
Just set a webview client and override onpagefinished as shown below
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(), url,Toast.LENGTH_LONG).show();
}
});
}
}
inside onpagefinished you have the url

WebView fails to load webpage

I have a WebView trying to load a WebPage like this:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv=(WebView)findViewById(R.id.webview);
WebSettings ws=wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
}
I have added the internet permission to the manifest
<uses-permission android:name="android.permission.INTERNET"/>
However the WebView is blank,it neither throws an error nor loads the web-page.How do I load webpages like this,I have only tried loading local html files before and I would like to know if I have to do something different.
Try like this way:
mWebView.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
mWebView.setWebViewClient(new HelloWebViewClient());
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Use this code .shouldOverrideUrlLoading() not use compulsary.use this code and please reply me this code work or not
Try below code.
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
setContentView(mWebview );
}
}

Android WebView zoom out after page is loaded

I have an initialized WebView with the following code:
_webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
while (view.zoomOut());
}
_webView.loadURL("path/to/image.png");
But this does not work. The WebView zooms out while loading. But when it is finished, it will zoom in.
Is there any other way to zoom out completely after a page (I use the WebView to display an image) is loaded?
Try this piece of code:
public class WebViewSampleActivity extends Activity
{
WebView wb;
private class HelloWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wb=(WebView)findViewById(R.id.webView1);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.getSettings().setPluginState(WebSettings.PluginState.ON);
wb.getSettings().setPluginsEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
wb.loadUrl("http://www.foofoo.com");
}
}
Hope this helps.
Don't do anything in onPageFinished() instead use webview.getSettings().setDefaultZoom(WebviewSetting.ZoomDensity.FAR);
Hope this helps
These are the two most important commands when loading images:
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);

Categories

Resources