can you please tell how to remove caching in webview in android. I open one url there one textfield in that I write
come text on that field and go next page .then come to same page write same thing it will open autosuggest (like thing which text I already used previously).
Thanks
here is my code
package com.firstgroup.ui;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ApplicationLoad extends Activity {
WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new MyBrowser());
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.loadUrl("http://192.168.11.90/loginuser/login");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
<RelativeLayout 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"
tools:context=".ApplicationLoad" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Try by putting this in OnCreate():
//context is Activity context
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
Also try this:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
Try following methods of Webview,
mWebView.clearFormData();
mWebView.clearCache(true);
And to remove the EditText's border you need to declare following property in your .xml file.
android:background="#null"
try this for not displaying autosuggetion
mWebView.clearFormData();
mWebView.getSettings().setSavePassword(false);
mWebView.getSettings().setSaveFormData(false);
// turn caching off
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
and use LOAD_CACHE_NORMAL for default behavior.
Related
I have a weird problem with Andriod WebView. My website's Humberger menu expands and closes fine in my mobile browser but on my Android WebView app when the menu is clicked it's not working (doesn't expand fully to show menu items).
I've already tried the following lines of code from various answers on StackOverFlow:
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
settings.setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
Mobile Browser Working Correctly:
Android WebView App Not Working Correctly
Here is my Andriod WebView App code:
package com.app.pvm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.MyWebView);
webView.setWebChromeClient(new MyChromeClient());
webView.setWebViewClient(new BrowserClient());
WebSettings settings = webView.getSettings();
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("https://test.app");
}
}
XML Code:
<?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"
tools:context=".MainActivity">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MyWebView"/>
</LinearLayout>
BrowserClient Class code:
package com.app.pvm;
import android.graphics.Bitmap;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
class BrowserClient extends WebViewClient {
public BrowserClient(){
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
Note: I'm using Andriod Studio for the WebView App.
your WebView should fit whole window, use match_parent
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/MyWebView"/>
wrap_content set for height makes this inner dropdown wrongly measured, in the meanwhile whole web content stretches WebView to size of its parent (LinearLayout) and became scrollable
Im working on my first android app. Im having a very strange problem with WebView Component of Android. My website have responsive design. The problem is it resize fine on Emulator but when i run on my device webview show normal design, not responsive. Any idea? this is my code:
MainActivity.java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView myWebView;
#SuppressWarnings("deprecation")
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.loadUrl("http://apelarse.com.ar");
myWebView.setWebViewClient(new myWebViewClient());
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if(myWebView.canGoBack()){
myWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
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"
tools:context=".MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
I copy the code from this site: http://samadmalik.com/converting-website-android-app/
Try something like that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
I hope it should work (even if webview parameters at first glance look a bit strange).
You might have to add this:
webview.getSettings().setUseWideViewPort(true)
This is my first app, it is a web app, it simply loads a webView to a url where stuff happens.
It works fine in android browser(gingerbread), but in Chrome(ICS, JB) it was going back to the initial webView url when the device is rotated. In gingerbread you account for this by setting android:configChanges="keyboard|keyboardHidden|orientation" and overriding onConfigurationChanged.
After searching the web, I re-wrote my original activity as per the example here: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/ , in which you essentially handle the re-orientation yourself.
However now my app is crashing when I rotate the device.
Testing with Android 4.1.2.
Anyone see anything off with my code? Sorry if the answer is obvious, but I've been stuck on this bug for a couple of days now. Maybe I just need another set of eyes on it. Thanks in advance!
Note: the line #SuppressWarnings("deprecation") is not in the example, it was suggested by the editor in order to compile.
The Main Activity:
package com.example.xxx.xxx;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
protected FrameLayout webViewPlaceholder;
protected WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
#SuppressWarnings("deprecation")
protected void initUI() {
webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));
if (myWebView == null){
//Create It
myWebView = new WebView(this);
myWebView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadsImagesAutomatically(true);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setInitialScale(1);
myWebView.loadUrl("http://www.theurl.com");
}
webViewPlaceholder.addView(myWebView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onConfigurationChanged(Configuration newConfig){
if (myWebView != null) {
webViewPlaceholder.removeView(myWebView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
initUI();
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
myWebView.restoreState(savedInstanceState);
}
}
Main Activity XML:
<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="horizontal" >
<FrameLayout android:id="#+id/webViewPlaceholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
try adding orientation to you activity definition in manifest file as follows:
<activity android:screenOrientation="portrait" ...>
</activity>
I realized I don't even need WebChromeClient at all if I'm not using any of it's features. Found answer here: How to prevent WebView auto refresh when screen rotation
simply add "screenSize" to your configchanges in manifest.
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
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)
}
}
I am trying to find a code that will do a popup at the initial start up on an installed app. Much like a changelog that is starting to appear in more and more apps.
I have found some similar codes, but being a beginner I haven't been able to figure out where to exactly put the code in and I always have tons of errors that still do not work once I try and fix them.
I am working in Eclipse with an android project, and I'm using a webview to show a website.
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">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="false"/>
</LinearLayout>
Java File:
package com.A2Ddesigners.WhatThe;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class Whatthe extends Activity {
WebView webview;
/** Called when the activity is first created. */
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(50);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://mdsitest2.com/");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Using My this code you can show popup on any type of view on intial time of activity.
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class PopupDispaly extends Activity {
private PopupWindow outComePopup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiwter_login);
final View anyView = findViewById(R.id.popo_up); // define heade your view
anyView.postDelayed(new Runnable() {
#Override
public void run() {
callFirstToolTip(anyView);
}
}, 5000); // hear you can put your delay time like 5000 mls
}
public void callFirstToolTip(View view) {
Log.i("Started Info","popup");
View layout = LayoutInflater.from(PopupDispaly.this).inflate(R.layout.popup_copy_delete, null); // define hear your layout file id.
LinearLayout layCopyDelete = (LinearLayout) layout.findViewById(R.id.layCopyDelete);// hear define your id of sub lay like LinearLayout.
outComePopup = new PopupWindow(layout);
outComePopup.setFocusable(true);
layCopyDelete.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
outComePopup.setWidth(layCopyDelete.getMeasuredWidth());
outComePopup.setHeight(layCopyDelete.getMeasuredHeight());
outComePopup.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
outComePopup.setOutsideTouchable(true);
outComePopup.showAsDropDown(view);
}
}
And my Custome View layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/got_it_bg"
android:layout_gravity="center_horizontal"
android:id="#+id/layCopyDelete"
android:layout_marginLeft="100dp"
android:orientation="horizontal">
</LinearLayout>
Sounds like you should use a custom dialog box. http://developer.android.com/guide/topics/ui/dialogs.html
There's a code example at the bottom of the page. To show it, you can call onCreateDialog(int) from inside your main activity in its onCreate() method.
PopupWindow.showAsDropDown() in a delayed process can solve this problem, you can execute the show popupWindow in onCreate() method by 0.5s delay.
You should use PopupWindow.showAtLocation but use the post method of the one of the views on your activity. you can also use the root view of the activity using findViewById(android.R.id.content).
You code should look like this:
View anyView = findViewById(R.id.anyView);
anyView.post(new Runnable()
{
#Override
public void run()
{
// Create and show PopupWindow
}
});