Problem with webview not loading - android

I am having a bad experience with webview which does not load the web page which I request.
I cannot load google or any other page with a webview. I have put in xml:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_marginLeft="250px"
android:layout_marginTop="80px"
android:layout_width="180px"
android:layout_height="160dip"
/>
I then put in the code:
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
This shows up stating the webpage is not available.
I have also added the permission to the manifest.
I have another activity within this application which loads a youtube url fine using:
startActivity(new Intent( Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=XS998HaGk9M")));// Starts an intent to watch the video
I'm not sure what this could be and really need advice on this as I need to get it working.
Thanks
Edit: I also cannot access any webpage within the actual emulator itself. By searching in the search bar within the emulator this says the same thing when connecting to Google.
I'm not sure why this would connect to youtube with an intent and not a webview
Edit: This is not even connecting to youtube now, it says the same as above. This is messed up as I need this to work for my project tomorrow. If the webview keeps going down this is not very reliable. I may have to change the device I'm working with as with android things keep going wrong.
Edit: I have just come back after a few hours without touching the code or the emulator and when I run the application the youtube video was back on and I can browse within the emulator. But I still cannot connect via webview. VERY UNRELIABLE :(

Make sure you have included Permissions for Internet Access
<uses-permission android:name="android.permission.INTERNET" />

I had the same problem. It seems to be solved when putting the webview code elsewhere than in the MainActivity.onCreate() method . For example, put the code containing webview.loadUrl(…) into the onClick method of a button. The WebView appears empty on launching the activity, and correctly filled when clicking the button .

Does the emulator have internet access? I have noticed similar behavior within the emulator at times and it is due to the emulator not starting up properly. The only work-a-round I've been able to come up with is to restart the emulator until it has internet access (usually one or two times).
-Dan

package com.Example.Browser;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieManager.getInstance().setAcceptCookie(true);//Enable Cookies
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);//Enable Java Script
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://www.google.com/"); //Set Home page
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//Remove ScrollBars
mWebView.getSettings().setDefaultFontSize(12);//Set Font Size
mWebView.getSettings().setLoadsImagesAutomatically(true);//Enable Image Loading
mWebView.getSettings().setPluginState(PluginState.ON);//Enable Flash
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); //improves Feedback on touch
//mWebView.setBackgroundColor(0x00000000);//Transparent Screen When Loading
//mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom Controls
//mWebView.getSettings().setDisplayZoomControls(false);//Always Hide Zoom Controlls(Requires Api 11)
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);//Set Cache (8mb)
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();//Set Cache (8mb)
mWebView.getSettings().setAppCachePath(appCachePath);//Set Cache (8mb)
mWebView.getSettings().setAllowFileAccess(true);//Set Cache (8mb)
mWebView.getSettings().setAppCacheEnabled(true);//Set Cache (8mb)
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//Set Cache (8mb)
mWebView.requestFocus(View.FOCUS_DOWN);//Enable WebView Interaction
//mWebView.setWebViewClient(new WebViewClient() {//Open URL on Error
//public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {//Open URL on Error
//mWebView.loadUrl("http://www.google.com");//Open URL on Error
//mWebView.loadUrl("file:///android_asset/error_404.jpg"); //Show Offline HTML file or Image on Error
// }
// });
}
private class HelloWebViewClient 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) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
<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/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Related

Discord OAuth2 login Fail via Android Webview

I'm using discord to let the user login on my android app.
The code is as follows:
MainActivity.xml
<RelativeLayout 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">
<FrameLayout
android:id="#+id/webview_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<WebView
android:id="#+id/authWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
app:layout_anchorGravity="center" />
</FrameLayout>
DiscordOAuth.java:
package com.example.oauth2test;
import android.app.Activity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
class DiscordOAuth {
private static final String LOG_TAG = "Discord OAuth 2 Login";
public void getAuthentication(Activity activity) {
String authURL = "https://discordapp.com/login";
WebView authWebView = activity.findViewById(R.id.authWebView);
authWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
authWebView.getSettings().setAppCacheEnabled(true);
authWebView.getSettings().setLoadWithOverviewMode(true);
authWebView.getSettings().setUseWideViewPort(true);
authWebView.getSettings().setBuiltInZoomControls(true);
authWebView.getSettings().setSupportMultipleWindows(true);
authWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
authWebView.setWebViewClient(new WebViewClient());
authWebView.loadUrl(authURL);
authWebView.setVisibility(View.VISIBLE);
}
}
when the above is executed, you will get a login page, but when you try to loging, the loading animation will appear for a moment, and you will be back on the login page without even a success or a failure
after searching, i suspect that the problem has something to do with multiple windows and how to deal with them, but i can't be sure.
The final goal is to use oauth2 via a WebView to login to discord and get the username of the user
Implement AuthCallback interface in your class & you will get response in callback methods
Discord internally needs WSS connection to complete authorization. WSS connection requires DOM storage to store client information. However in Android webview, DOM storage is disabled by default. you can change this in WebSettings by webSettings.domStorageEnabled = true

Convert my localhost website into android app

I want to create a web view of a localhost website or not online website how can I do that its picking online websites link but not localhost is there a way to do this?
I have tried giving the link using localhost and ip but it’s not picking my website.
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
webSettings.getJavaScriptEnabled();
mywebview.loadUrl("http://192.000.00.0/traveland/index.php");
mywebview.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed()
{
if(mywebview.canGoBack())
{
mywebview.goBack();
}
else
{
super.onBackPressed();
}
}
}
MainActivity.xml
<RelativeLayout 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="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
android:id="#+id/webview"/>
</RelativeLayout>```
It shows all the online websites not the offline ones how can that be done?
Your pc and android must be in the same network, once you have turned on your web server (WAMP, XAMPP, Etc.) configure the vitualhots, with the firewall disabled from your pc.
If your localhost is working well and you want to make it an app that connects with the server localhost through your android then download the "ngrok" on your PC. there are tons of tutorials that will teach you how to host localhost websites online once you do that you are going to paste that ngrok URL which is online version of your website and then name your app give your email and just submit like a normal form and you will be receiving email and there will be two links. one for public link sharing of your app and another is private app management which will allow you to change some functions of your app like URL
and others just change the URL to your localhost URL and then you will be able to access your contents of localwebsite without ngrok keep in mind if you change layout of your web application then you must update your app through same process again
link for making app with this website: https://gonative.io/

Android Studio: How to set a webview loading and error view?

I am working on an application for my study purpose , the app contain a navigation drawer and some webviews.
my problem is when the page is loading, the screen still white and boring. and if the connection failed the ugly "no connexion" page appear showing my host adresse..
I want to set a loading page or image and an other one for alternative scenario(connexion fail, ...)i tried some solutions but no one of then suite my code.
the is the fragment JAVA and XML code..
layout1_acceuil.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>​
menu1_acceuil.java
package xxxx.yyyyyy;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Malek Boubakri on 13/03/2015.
*/
public class menu1_accueil extends Fragment {
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle setInitialSavedState) {
rootview = inflater.inflate(R.layout.layout1_accueil, container, false);
//set up webview
WebView webView1 = (WebView) rootview.findViewById(R.id.webView1);
// Enable Javascript
WebSettings webSettings = webView1.getSettings();
webSettings.setJavaScriptEnabled(true);
//load an URL
webView1.loadUrl("http://myhost.tn");
// Force links and redirects to open in the WebView instead of in a browser
webView1.setWebViewClient(new WebViewClient());
return rootview;
}
}
may my problem can seen easy for some genuis but am just a beginer and if this app dont run it will effect my mark badly :'( please post any information can help me and i will be thankful.
(Again,I have to mention that i still beginner in android dev and my English may be somehow bad..)
You can perform any action before and after the webpage is loaded with the class WebViewClient. You can override method like onPageStarted(), onPageFinished() and onReceivedError().
This is the android developer site for the WebViewClient, you can find all the information here: http://developer.android.com/reference/android/webkit/WebViewClient.html

Disable address bar in Android webview

How do disable and hide the address bar from a WebView?
There is no address bar in a WebView.
If you think you have a WebView, and you see an address bar, that is not your WebView. Rather, you are looking at the Browser application. Most likely, the URL you told the WebView to load did a redirect, and you did not intercept that redirect using a WebViewClient and shouldOverrideURLLoading().
Adding myView.setWebViewClient(new WebViewClient()); disabled the address bar for me.
import android.webkit.WebView;
import android.webkit.WebViewClient;
...
WebView myView = findViewById(R.id.myExampleView);
myView.setWebViewClient(new WebViewClient());
myView.getSettings().setJavaScriptEnabled(true);
myView.loadUrl("https://www.stackoverflow.com");
XML Snippet
<WebView android:id="#+id/myExampleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:gravity="center" />
source: (Japanese site):
http://www.techdoctranslator.com/android/webapps/webview
Finally I Try with this. Its worked for me..
Here is the working code
private WebView webview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ebook);
//webview use to call own site
webview =(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true);
webview.loadUrl("http://www.google.com");
}
and your entire main.xml(res/layout) look should like this:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
don't go to add layouts.
webview.setWebViewClient(new WebViewClient());
solved the problem for me..
Kotlin code as following
myWebView.setWebViewClient(WebViewClient())

Load an SWF into a WebView

I'm having problems with this. If I go to an SWF directly in the browser, it works fine. If I attempt to use loadUrl on an SWF file it stays blank and loads nothing.
Figured it out. You have to enable plugins.
webview.getSettings().setPluginsEnabled(true);
Niky, you have a code example here.
I have used this example to test this code and confirm it works. In this example the qualibus.swf is in contained within the assets of the app. Please test this on an actual device, as on the emulator it show a blank page (probably the flash player is not present on the emulator)
Test3Activity.java:
package com.blabla.test3;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Test3Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="file:///android_asset/qualibus.swf";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setPluginsEnabled(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"
>
<WebView android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
Result:
The function WebView.getSettings().setPluginsEnabled(); method has
been deprecated since API level 9, and was removed in API level 18.
You can use the newer function
WebView.getSettings().setPluginState(WebSettings.PluginState.ON);
which was added in API level 8 and was deprecated in API level 18.
According to the WebSettings Documentation API levels beyond 18 will
not support plugins; I'm assuming it's because the main plugin to
support was flash which adobe is no longer developing for mobile.
Quoted from source
So, for now you can use it till 18, and handle compatibility with higher APIs (sadly)

Categories

Resources