Trying to use WebView, I have the following code:
<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:orientation="vertical"
tools:context="com.example.places.xt.MainActivity$PlaceholderFragment" >
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
and
(...)
import android.webkit.WebView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
WebView webview = (WebView) findViewById(R.id.webview);
setContentView(webview);
String summary = "<html><body style=\"background-color: yellow\">You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
}
(...)
When I click on "Run" to force building APK file and then download and start it on my phone, it says "App has been stopped". The app has the size of only about 760 kB. I think it's not very much, I guess the WebView package is not correctly imported. What can be the reason?
Tested on 4.4 Kitkat, Eclipse, Win 7 32 bit
First, your webview is in the fragment layout and not in the activity layout. Move the code that accesses the webview to the fragment's onCreateView(). See NullPointerException accessing views in onCreate().
Second, remove the setContentView(webview). It serves no practical purpose to find a view from the current content view and set it as the content view.
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.
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'm trying to play YouTube video in my application using WebView.
But when I click "Play" the application crashes.
This is my code:
public class Youtube extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_video_item);
WebView videoView = (WebView)findViewById(R.id.videoView);
TextView tvTitle = (TextView) findViewById(R.id.VideoTitle);
WebSettings videoViewSettings = videoView.getSettings();
videoViewSettings.setJavaScriptEnabled(true);
videoViewSettings.setPluginState(WebSettings.PluginState.ON);
videoView.setWebChromeClient(new WebChromeClient());
String youtubeID ="XSzE2LLFluE";
tvTitle.setText(youtubeID);
videoView.loadUrl("http://www.youtube.com/embed/" + youtubeID +"?autoplay=1&vq=small");
}
}
And this is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/VideoTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextView
android:id="#+id/VideoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/imgdesc"
android:layout_marginRight="10dp"/>
</RelativeLayout>
I'm testing the project with Nexus 5 on Android Studio emulator.
You have to add the two lines of code:
webView.getSettings().setPluginsEnabled(true); webView.getSettings().setJavaScriptEnabled(true);
this will solve the problem. Try it and tell.
I found the issuse.
Beacuse I did the tests on Emulator without Youtube app its crashes.
On a device with Youtube app its working fine.
Now its crasheswhen i click on 'Full screen' button
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
I want to create a background that covers the whole screen of my Activity.
Just like:
android:background="#drawable/background"
But I want this background to be animated (60 frames), looping and target as many device resolutions as possible.
I tried to do a drawble .jpg sequence, but the problem is that I use 1920x1080, and the logcat while trying to test it on the emulator, the application crashes, and LogCat gives me en error saying, not enough memory
Then I tried to import a video of my sequence and set it to play on the background. But I don't know where to put the video in my res folder and call it from there with SetVideoPath(), in order to play it.
What is the best approach for an animated background, that covers multiple devices with different resolutions?
#Arun C Thomas
I still get an error in logcat,
OutOfMemoryError
my code is this:
package combiolab.biolab;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.view.WindowManager;
public class MainActivity extends Activity {
private Renderer graphicsRenderer;
//public static String gl;
AnimationDrawable anim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
GLSurfaceView myGLSurfaceView = (GLSurfaceView) findViewById(R.id.gl);
myGLSurfaceView.setEGLConfigChooser(true);
myGLSurfaceView.setRenderer(graphicsRenderer);
}
<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:gravity="center"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.opengl.GLSurfaceView
android:id="#+id/gl"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</android.opengl.GLSurfaceView>
</RelativeLayout>
You need to create an OpenGL view,
then add it like
<android.opengl.GLSurfaceView
android:id="#+id/gl"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</android.opengl.GLSurfaceView>
In onCreate() implement Renderer like
GLSurfaceView myGLSurfaceView = (GLSurfaceView) findViewById(R.id.gl);
myGLSurfaceView.setEGLConfigChooser(true);
myGLSurfaceView.setRenderer(graphicsRenderer);