i have the Problem that i load a website with the loadDataWithBaseURL method. There i load a prepared internetsite in a webview. if i go back in my previous activity and start the webview activity it loads the same prepared website with loadDataWithBaseURL() method but i get a webview with twice the content. For example Hello World and the second Hello World.
My Code is the followed:
package de.http.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebBackForwardList;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends Activity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mywebview);
Bundle bundle= this.getIntent().getExtras();
String param1 = bundle.getString("Weburl");
getDetailWebsite(param1);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
loadWebview();
}
public void loadWebview()
{
webview.clearView();
webview.loadDataWithBaseURL("http://branchenportal-schlitz.de", HttpHelperClass.htmlDetailWebsiteCode, "text/html", "UTF-8", HttpHelperClass.htmlDetailWebsiteCode);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
WebBackForwardList mWebBackForwardList = webview.copyBackForwardList();
String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()).getUrl();
if(historyUrl.contains("<html>"))
{
loadWebview();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private void getDetailWebsite(String url)
{
try{
HttpHelperClass.executeHttpGetDetailWebsite(url,this);
}catch (Exception ex)
{
ex.printStackTrace();
}
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
if(url.contains("branchenportal-schlitz.de"))
{
view.clearHistory();
}
super.onPageFinished(view, url);
}
}
#Override
protected void onStop() {
webview.clearCache(true);
webview.clearView();
webview.clearHistory();
webview.destroy();
super.onStop();
}
}
Is there anybody who knows what iam doing wrong?
Thanks in advance.
With regards
OK well to trouble shoot this I would start by getting rid of (commenting out) all of your code below public boolean onKeyDown(int keyCode, KeyEvent event). Then you can see if its in your onCreate or not. But that's up to.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.buttons);
wb = new WebView(this);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
final String urlToLoad = "http://www.yourWebsite.com";
wb.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
wb.loadUrl(urlToLoad);
}
start here then start adding your extra code back in.
Related
public class WebViewActivity extends AppCompatActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webview = new WebView(this);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
}
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
return true;
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://stackoverflow.com")) {
return false;
}
return true;
}
}
I want to block an url in the WebView, how to block the url? This code doesn't work.
You should return true if you want specific behavior according to documentation.
Returns true if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
block all the urls working solution even i tried it.
public class MainActivity extends Activity {
private WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv1 = new WebView(this);
setContentView(wv1);
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl("http://www.kart.epizy.com");
wv1.setWebViewClient(new MyWebViewClient());
wv1.setClickable(true);
}
public class MyWebViewClient extends WebViewClient {
public boolean shuldOverrideKeyEvent(WebView view, KeyEvent event) {
// Do something with the event here
return true;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return false to allow urls or add your condition like
//return url.equals("https://play.google.com/store/apps/developer?id=kArt+Developers");
return true;
}
} }
I would like to be able to upload a file from an Android 4.x device, whenever I select "choose file" nothing happens.
It works when I use Chrome directly but not with the WebViewClient from within the app.
My code:
public class ActivityWebView extends MainActivity {
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String websiteURL = "url";
wv = (WebView) findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(websiteURL);
wv.setWebViewClient(new WebViewClientClass());
wv.getSettings().setSupportZoom(false);
wv.getSettings().setBuiltInZoomControls(false);
}
public class WebViewClientClass extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if ((KeyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack())
{
wv.goBack();
return true;
}
return super.onKeyDown(KeyCode, event);
}
im working on my first android app, be patience. In my android emulator, the responsive design works fine, but when i create the apk and run in my device the design is normal, not responsive. Any idea?
This is my code:
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);
}
}
please try from this and i think if web responsive layout not working then its not android side probe its web side probe first you try from my code may be it will help you.
public class WebViewActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
private TextView header_maintext;
private TextView headeroptiontext;
private RelativeLayout back;
private String url_string="http://www.google.com";
private String header_maintext_string="tittle";
/** Called when the activity is first created. */
#SuppressLint("SetJavaScriptEnabled") #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview_layout);
url_string = getIntent().getStringExtra("url");
header_maintext_string = getIntent().getStringExtra("header_text");
webview = (WebView)findViewById(R.id.webview01);
header_maintext= (TextView)findViewById(R.id.header_maintext);
header_maintext.setText(header_maintext_string);
headeroptiontext = (TextView)findViewById(R.id.headeroptiontext);
headeroptiontext.setVisibility(View.GONE);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
back = (RelativeLayout) findViewById(R.id.back_layout);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(webview.canGoBack() == true)
{
webview.goBack();
}
else
{
finish();
}
}
});
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebViewActivity.this, "tittle", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl(url_string);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
i want to disable phone browser and i want to use a webview .
but this code didn't work for me . please guide me :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Wop=(WebView)findViewById(R.id.webi);
String mturl="http://www.google.com";
Wop.getSettings().setJavaScriptEnabled(true);
Wop.loadUrl(mturl);
}
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://www.stackoverflow.com");
To enable the previous web-page to be loaded,
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
} }
Definiton of the HelloWebViewClient class :
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
//what you want to do when the page finished loading, eg. give some message, show progress bar, etc
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
//what you want to do when the page starts loading, eg. give some message
}
}
WebView w = new WebView(this);
//add this to your code
w.setWebViewClient(new Callback());
w.loadUrl("http://www.facebook.com");
//and this class also
private class Callback extends WebViewClient{ //HERE IS THE MAIN CHANGE.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
I would like to find a solution to allow my WebView still alive when screen time-out.
I am program to play music using WebView, it's working very properly if the screen alive but when the screen time-out my music in WebView also GONE.
Here is my code:
public class WebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://mp3skull.com/usher.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onPause()
{
mWebView.setVisibility(View.GONE);
mWebView.destroy();
super.onPause();
}
}
I am very shy to tell you that i tried to find this solution over a week but still can't done :(.
Hope you give me solution on this matter.
Thanks you.
Remove onPause() method in your class like below code:
public class WebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://mp3skull.com/usher.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}