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
Related
I'm new on programming. I'm trying to create an app with Webview/Android.
I'm trying to add a type of splash image until the web page is finished loading.
I got it the splash image, but the bliss ends before the url is finished loading. Then I get a white screen before the page is displayed. I tried many different guides, but none of them solve the problem. I hope some one can help me with this.
I want splash or an images to display while the web page finished loading and showing page without any white screen in between.
Here is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
MainActivity.java
package com.eatnow.brasil
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//webview use to call own site
webView =(WebView)findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());//use to hide the address bar
webView .getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true); // Enable zoom on sites
webView.getSettings().setSupportZoom(true);
webView .getSettings().setTextZoom(95); // where 90 is 90%; default value is ... 100
webView .getSettings().setDomStorageEnabled(true); //to store history
webView.setBackgroundColor(0xff2f0848);
//imporove webView performance
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSaveFormData(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadUrl("https://eat-now.no/");
}
#Override
public void onBackPressed() {
if(webView.canGoBack())
{
webView.goBack();
}
else{
super.onBackPressed();
}
}
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#2f0848"
tools:context=".SplashActivity">
<ImageView
android:id="#+id/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:contentDescription="#string/app_name"
android:src="#drawable/eatnow_splash" />
</RelativeLayout>
This is a typical problem of WebView.
We normally use a ProgressDiglog to fill the white screen when webView loading.
Here is a example using ProgressDialog when the WebView is loaging:
https://github.com/rupok/webview
Its a old project using eclipse IDE, but you could refer the ShowWebView.java and the xml in layout. You could use a image to replace the ProgressDialog, if you want.
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.
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
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">
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>