WebView not showing website correctly - android

WebView is not showing website correctly. Any help would be great!
The code ive used work on all another site. Not sure whats the issue. Any thing I should add? Works well in chrome and other browsers so don't know what to do. Any help would be great!
WebView
Chrome
public class Website extends Activity {
WebView myWebView;
LinearLayout root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.website);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://dspart.org");
root = (LinearLayout) findViewById(R.id.root);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dspart.org")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack()) {
myWebView.goBack();
}
else {
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
root.removeView(myWebView);
myWebView.removeAllViews();
myWebView.destroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}}

YourWebView.getSettings().setJavaScriptEnabled(true);
try enabling javascript on your webview! code snippet above :

Seems like there is an error in the following line
if (Uri.parse(url).getHost().equals("http://dspart.org"))
getHost will return dspart.org and not the url with http. Try after modifying it. Thanks
Refer - http://developer.android.com/reference/android/net/Uri.html#getHost()

Related

Don't navigate to other pages in WebView,disable links and references and go back to first page

I have a webView in Android and I open a html webpage in it. But it's full of links and images: when I click one of them it loads in my webview.I want to disable this behaviour, so if I click on a link, don't load it and go back to the first page.
I've tried this solution and edited a bit for myself, but it does not work:
webSettings.setJavaScriptEnabled(myWebViewClient.equals(true));
This opens a white page but I want to open the main URL. My webviewclient code:
public class MainActivity extends Activity {
public static String URL = "http://www.example.com/";
private WebView webView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webview);
MyWebViewClient myWebViewClient = new MyWebViewClient();
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(myWebViewClient.equals(true));
webView.reload();
webView.loadUrl(URL);
webSettings.setDisplayZoomControls(true);
webView.setWebViewClient(new WebViewClient());
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
return true;
}
break;
}
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}
else{
super.onBackPressed();
}
}
}
you need to overwrite onPageStarted in WebViewClient:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//here check url i.e equals to your that html page
// if url equals not load url
and for go to back page check:
if(view.canGoBack())
// load first page url
}
You need a WebViewClient that overrides loading url:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// url is the url to be loaded
return true; // override url (don't load it)
return false; // let url load
}
});
You can always return true to stay on the same page.
To open Main Url and stay on it, use this:
webview.loadUrl(MainUrl);
and then override loading url and always return true:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});

Android : Magento 2 e-commerce Website not Working in android Webview

When I am trying to load a Magento 2 e-commerce website in android WebView, i'm getting following error message:
"Unable to send the cookie. Maximum number of cookies would be
exceeded."
What's the cause of this error.? How can I fix this.?
Could anyone help me.?
Android Code
WebView webView=(WebView) findViewById(R.id.disp);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new MyViewClient());
try {
webView.loadUrl("https://sweetroomksa.com/");
}catch (Exception e){
}
Create your custom WebViewClient class and then try to load WebView url like below:
WebViewClientImpl.java
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.indexOf("/sweetroomksa.com/") > -1 )
return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("https://sweetroomksa.com/");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Above code working perfectly in my device.
Hope above answer may help you.

Android WebView Stops while clicking Back

My webview module isn't working. Whenever I try to navigate back from any webpage, the app crashes giving following error
"Unfortunately the app has stopped".
Can anyone please explain what is the reason?
public class MainActivity extends ActionBarActivity {
WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://nahdaschools.com/v4/index.php/apps");
}
private class MyCustomWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed() {
if (myWebView.copyBackForwardList().getCurrentIndex() > 0) {
myWebView.goBack();
}
else {
// Your exit alert code, or alternatively line below to finish
super.onBackPressed(); // finishes activity
}
}}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack()){
myWebView.goBack();
}
else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
with this you will be able to come on your last activity.
#Override
public void onBackPressed() {
super.onBackPressed();
moveTaskToBack(false);// finishes activity
}
}}

WebView also Gone when Sreen Time-Out

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);
}
}

Stop the Music in WebView when Screen Time-Out

I am programming that allow users can listen music in WebView via the URL web link.
But I wonder why when the android phone user screen Time-Out my music in WebView also stop.
Do you have any solution to solve this problem?
Here is my code:
public class PlayMusicActivity 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://www.myweb.com/music.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();
}
}
Appreciate for your help.
Best regards,
Virak

Categories

Resources