Android App not connecting to internet on emulator - android

The app does not connect to the internet. I have
<uses-permission android:name="android.permission.INTERNET" />
in my manifest and I checked the other applications on the emulator for example youtube and google search works but the application isn't able to load the page. This is my main java file.
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebview ;
myWebview = (WebView) this.findViewById(R.id.webz);
myWebview.loadUrl("https://en.wikipedia.org/");
}
}
and this is my main activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.hoda.myapplication.MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webz"></WebView>
</android.support.constraint.ConstraintLayout>
this is the error i get
[ERROR:gl_surface_egl.cc(289)] eglChooseConfig failed with error EGL_SUCCESS

Try enabling javascript and override the webviewclient to avoid redirection:
WebView myWebview ;
myWebview = (WebView) this.findViewById(R.id.webz);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.setWebViewClient(new WebViewClient());
myWebview.loadUrl("https://en.wikipedia.org/");
and update the constraints of your WebView:
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/webz"></WebView>

The problem was with the emulator's graphics I believe as trying #diegoveloper 's solution on the emulator still did not load the page but trying it on an actual android device was successful.

Do this
Restart the emulator (as suggested in another post and in the comments of this post)
If that doesn't work, restart your device.
Worked for me.

Add activity in your AndroidManifest.xml
android:name="com.(your browser activity)"
android:screenOrientation="portrait">

Related

WebView is clipping the webpage in android 4.4

WebView is clipping the webpage on Android 4.4. Here is how it looks:
It is opening correctly in google chrome. I want it to open like this in the webview.
Following is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView .getSettings().setLoadWithOverviewMode(true);
webView .getSettings().setUseWideViewPort(true);
webView.loadUrl("https://viftbox.com/");
}
}
For using a recent WebView engine on Android and you can't use Android System WebView or Google Chrome:
Prior to Android 5.0 improvements to the Android WebView was limited to OS upgrades/updates. So the recommendation was to use the NOW DEPRECATED Crosswalk Project as the WebView. While the site no longer exists, its ghost exists on archive.org I don't know if enough of the site survives there to be useful. This might be a solution if you are unable to change hardware and need more recent webkit features.
As the Crosswalk Project is no longer updated, or it doesn't have the features you need, I've learned that Mozilla has a GeckoView which wraps their Gecko render engine.
Note that according to Mozilla's documentation they have their own APIs so GeckoView is NOT a drop-in replacement for Android's WebView, so some effort must be made for integration.
Either way including a custom webview engine will add to the APK size.

loadUrl() crashes with CordovaWebView

I know that there are some posts here but they are not solving my problem.
The thing is:
I have a Cordova App (android platform). In my activity_main.xml I have declared a CordovaWebView and I want to call loadUrl() method to load a website that is in my assets folder. When I invoke the method the app crashes showing "UNFORTUNATELY, MyApp HAS STOPPED".
STRANGE THINGS I HAVE NOTICED:
If I emulate using a device with API 10 or 16, this WORKS FINE. If I test it with 17 it does NOT WORK.
If I don't use CordovaWebView and directly invoke: this.loadUrl("file:///android_asset/www/index.html"); it works fine, but it's like I am not using the layouts I define (so I don't have my ad banner).
Any ideas about this problem??
Thanks!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/layout_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/layout_body"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_above="#+id/layout_banner"
android:background="#color/abc_search_url_text_normal">
<org.apache.cordova.CordovaWebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/common_signin_btn_dark_text_disabled" />
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_banner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/wallet_hint_foreground_holo_dark">
</RelativeLayout>
</RelativeLayout>
MainAcitivy.java
public class MainActivity extends CordovaActivity {
CordovaWebView cwv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
..LOAD ADVERTISEMENT INTO LAYOUT_BANNER...
cwv = (CordovaWebView) findViewById(R.id.webview);
cwv.getSettings().setJavaScriptEnabled(true);
cwv.loadUrl("file:///android_asset/www/index.html");
}
look the doc : https://cordova.apache.org/docs/en/3.5.0/guide_platforms_android_webview.md.html
cwv = (CordovaWebView) findViewById(R.id.cordovaview);
Config.init(this);
cwv.loadUrl(Config.getStartUrl());
you can see a sample here : https://github.com/dam1/sample-android-cordova-webview
Why do you need activity_main.xml and all android stuff, just start a fresh project using cordova command line. You don't need any xml of that sort in standard cordova project.
You've got cordova plugins for displaying admob ads inside cordova web view, see http://plugreg.com/search?q=admob

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)

Why does Android WebView display a black screen?

I've been banging my head against a wall this afternoon trying to get a WebView to work. Below is the code in the main class:
public class fkyougoogle extends Activity {
/** Called when the activity is first created. */
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
// WORKS
//webview.loadUrl("http://www.google.com");
// DOESN'T WORK
//webview.loadUrl("http://www.theregister.co.uk");
//webview.loadData("<html><body>hello</body></html>", "text/html", "utf-8");
//webview.loadDataWithBaseURL("fake://", "<html><body>hello</body></html>", "text/html", "utf-8", "http://www.theregister.co.uk/");
}
}
This is Google's "Hello, Webview" example. If I use a WebView and try to access www.google.com then it works fine. If I try to access any other site then it fails including loadData and it just displays a black screen in the emulator. In the end I would like to read from a local file.
is included under the manifest tag and the XML schema is the same as the Hello Webview example.
Am I missing something obvious here? :(
Try changing
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to
android:layout_width="fill_parent"
android:layout_height="fill_parent"
in your main.xml top level LinearLayout
It should look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Try UTF-8 instead of utf-8 for your latter two attempts. I have no problem loading http://www.theregister.co.uk using the same code -- try loading it in the built-in Browser app, and if that fails, you're perhaps encountering some sort of firewall/proxy issue.
Here are a few projects demonstrating simple uses of WebView, from one of my books.
I had similar problem of a WebView being completely blank, but it my case it was caused by a missing android.permission.INTERNET uses-permission.

Categories

Resources