How to remove the white screen before the webview load? - android

I am creating an app that loads a website through the webview, and before it shows a splash screen. The problem is that after the splash screen a white screen appear and then the webview loads.
I don't want to use a timer in the splash scree, I want it to be gone once the webview is loaded. I saw that I need to move the splash activity to the main activity, but I am not sure how. I am a beginner with android studio.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.atlasdatabase">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
<activity
android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.NoActionBar">
</activity>
</application>
</manifest>
MainActivity.java:
package app.atlasdatabase;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import static app.atlasdatabase.R.style.AppCompatAlertDialogStyle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
WebView myWebView = (WebView) findViewById(R.id.atlasdatabase);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
myWebView.loadUrl("file:///android_asset/index.html");
}
/**
* Exit the app if user select yes.
*/
private void doExit() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this, AppCompatAlertDialogStyle);
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.setTitle(R.string.exit);
alertDialog.setMessage(R.string.exitmsg);
alertDialog.setNegativeButton(R.string.no, null);
alertDialog.show();
}
#Override
public void onBackPressed()
{
WebView webView = (WebView) findViewById(R.id.atlasdatabase);
if(webView.canGoBack()){
webView.goBack();
}else{
/* Close the app without the Dialog
super.onBackPressed();
*/
/* Use the dialog to Exit the App */
doExit();
}
}
}
SplashActivity.java:
package app.atlasdatabase;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
activity_main.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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context="atlasdb.atlasdatabase.MainActivity">
<WebView
android:id="#+id/atlasdatabase"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_home_footer">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
background_splash.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background color -->
<item android:drawable="#color/colorPrimary"/>
<!-- Image at the center of the screen -->
<item>
<bitmap android:gravity="center" android:src="#drawable/splash"/>
</item>
</layer-list>

I found the simplest answer in
How to manage the Blank White Loading screen of an Android Webview?, which helped me to solve the problem I was facing without any Splash activity Screen:
In Android manifest:
<activity android:name=".MainActivity"
android:theme="#style/Splash">
...
and on Activity, after creating webView, set the background to be transparent.
myWebView = (WebView)findViewById(R.id.webView);
myWebView.setBackgroundColor(Color.TRANSPARENT);
(I have added progress bar popup and killed on pageFinished for users to know loading is in progress.)

Here in the answer you can see comments where you need to concern.
You don't need a SplashActivity for this.
You can keep a view which is match_parent to the root view (so it will go full screen) and add your image or whatever to that.
When your Activity loads make sure it is visible/or like in the code given # onPageStarted if the view is not loaded yet, you can display your logo..
When the web view is ready as in the comment # onPageFinished make your splashView invisible or view gone!
for these tasks you can use android:visibility="gone" in Xml, yourRootViewWithImage.setVisibility(View.VISIBLE);
yourRootViewWithImage.setVisibility(View.GONE); use these lines in proper places!
Credit goes to this post, example :
public class MainActivity extends AppCompatActivity {
private boolean loadingFinished = true;
private boolean redirect = false;
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
myWebView = (WebView) findViewById(R.id.atlasdatabase);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
myWebView.loadUrl("file:///android_asset/index.html");
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(
WebView view, WebResourceRequest request) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
myWebView.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED // HIDE YOUR SPLASH/LOADING VIEW
} else{
redirect = false;
}
}
});
}
/// below code ..
}

I've experienced this problem before and this is just a hack , but i had the same problem and for some reason that i'm still reading why but changing the web view from opening from a fragment to an activity made the white screen at the start of loading the web view went away.

Related

Android Main activity with 2 button to open the 2nd activity in webview

Hi I am new to android.
My main activity has show 2 buttons. If user click button 1 it to open google website in webview and for button 2 to open yahoo website in webview. I do not want open in any other browser.
Here is my code; but it is not working can any one please tell me what am I doing wrong here.
package com.jo.mavselect;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class SelectRm extends Activity {
public String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_rm);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.select_rm, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when the user clicks the Send button */
public void sendMessage(View v) {
if(v.getId()==R.id.B9)
{
message ="www.google.com.au";
}
else
if(v.getId()==R.id.B10)
{
message ="www.google.com.au";
}
Intent intent = new Intent(this, MavisActivity.class);
intent.putExtra("msg",message);
startActivity(intent);
}
}
**MavisActivity **
package com.jo.mavselect;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
public class MavisActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
setContentView(mWebView);
mWebView =(WebView) findViewById(R.id.Webview);
mWebView.setWebViewClient(new WebViewClient());
// web page to fit to the screen
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
// Not to cache
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.getSettings().setAppCacheEnabled(false);
// Enable Java script
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// No to cache
mWebView.clearCache(true);
//Clears any saved data for web forms.
mWebView.clearFormData();
//Tells this WebView to clear its internal back/forward list.
mWebView.clearHistory();
Intent intent = getIntent();
String ur = intent.getExtras().getString("msg");
mWebView.loadUrl(ur);
final Activity activity = this;
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
}
});
}
}
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jo.mavselect" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jo.mavselect.MavisActivity"
android:label="MavisActivity"
android:parentActivityName="com.jo.mavselect.SelectRm" >
// android:label="#string/app_name" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.jo.mavselect.SelectRm" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
activity xml file
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".SelectRm/">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goole"
android:id="#+id/B9"
android:layout_marginTop="60dp"
android:layout_alignParentTop="true"
android:onClick="sendMessage" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yahoo"
android:id="#+id/B10"
android:layout_toEndOf="#+id/B9"
android:layout_marginLeft="84dp"
android:layout_alignTop="#+id/B9"
android:layout_toRightOf="#+id/B9"
android:onClick="sendMessage" />
<WebView
android:id="#+id/Webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
You make totally wrong way.
You should try to fix your layout. Remove this line code mWebView = new WebView(this);
setContentView(mWebView); And replace with setContentView(R.layout.activity_xml); In xml file add this line code in your web view element. android:layout_below="#id/B9"
You must add onClickListener method for those buttons. In this listener set what page to open with web view.
Add method in your activity class:
public void sendMessage(View v)
{
if(v.getId() == R.id.B9){
mWebView.loadUrl("http://www.google.com");
}
if(v.getId() == R.id.B10) {
mWebView.loadUrl("http://www.yahoo.com");
}
}

Not able to play video in Web View

i am not able to play video on Android web view.
I have kept the html and video file in my assets folder.
Whenever i load the html file , it gives me the error
05-01 12:31:16.092: E/MediaResourceGetter(17241): Unable to read file: file:///android_asset/MediaBook2%20(2)/2B952499A0E681.mp4
And whenever i press on the play button i get the following error
05-01 12:31:23.680: E/chromium(17241): [ERROR:webmediaplayer_android.cc(328)] Not implemented reached in virtual void content::WebMediaPlayerAndroid::setRate(double)
05-01 12:31:23.710: E/MediaPlayer(17241): error (1, -2147483648)
05-01 12:31:23.710: E/MediaPlayer(17241): Error (1,-2147483648)
Am able to load any remote video and run,But problem is when i load the local video from the assets folder
Code to load the files and setup the web view
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Remove title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_webview);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
// Keep the webview setup ready
setupWebView();
}
public void setupWebView()
{
webView = (WebView) findViewById(R.id.webView);
// progressBar = (ProgressBar) findViewById(R.id.progressBarForWebView);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.setSoundEffectsEnabled(true);
webView.setWebViewClient(new SLCWebViewClient());
webView.setWebChromeClient(new WebChromeClient());
loadContentsInWebView();
}
public void loadContentsInWebView()
{
String localURL = "file:///android_asset/MediaBook2 (2)/SampleForVideo.html";
logger.debug("WebView URL: {}", localURL);
try {
webView.loadUrl(localURL);
}
catch (Exception e) {
e.printStackTrace();
logger.error("Error while loading url", e);
}
}
private class SLCWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.setWebChromeClient(new WebChromeClient()
{
private View mCustomView;
#Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
});
webView.loadUrl(url);
return true;
}
The Sample For Video.html code
<!DOCTYPE html>
<html>
<title>Testing for Video</title>
<body>
<video width="320" height="240" controls>
<source src="2B952499A0E681.mp4">
</video>
</body>
</html>
Code for the layout file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/fullscreen_custom_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000"/>
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
cheers,
Saurav
Thanks to Marcin for his answer.
I could run the the html files by loading the video.
My problem was i was using /MediaBook2 (2)/SampleForVideo.html. But the '/' should be removed when loading from assets. I splitted the string by trimming off the '/' and it worked.
But that was just a sample scenario i was working on to clear my understanding.
I have a much bigger folder structure and now when the .mp4 file is eventually loaded.
The media player is shown but the player is not playing any file.
The file:///android_asset protocol is a WebView-specific thing. That is: other system components can't read those URLs.
The MediaResourceGetter doesn't use the WebView's network stack and therefore doesn't "understand" the file:///android_asset protocol.
In your other question you mentioned you use a local http server - try serving the .mp4 from that.
if have still a problems about play video on android webview in 2018, let's give a chance and try code below.
Java:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);
final WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) {
webview.loadUrl("javascript:(function() {
document.getElementsByTagName('video')[0].play();
})()");
}
});
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://html5demos.com/video");
}
#Override
protected void onPause() {
super.onPause();
webview.onPause();
}
#Override
protected void onResume() {
webview.onResume();
super.onResume();
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.com">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:hardwareAccelerated="true"
android:allowBackup="false"
android:icon="#mipmap/logo_example"
android:label="#string/app_name"
android:roundIcon="#mipmap/logo_example"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
References:
https://gist.github.com/aprock/5913322

Prevent Chrome from reloading page on re-orientation?

This is my first app, it is a web app, it simply loads a webView to a url where stuff happens.
It works fine in android browser(gingerbread), but in Chrome(ICS, JB) it was going back to the initial webView url when the device is rotated. In gingerbread you account for this by setting android:configChanges="keyboard|keyboardHidden|orientation" and overriding onConfigurationChanged.
After searching the web, I re-wrote my original activity as per the example here: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/ , in which you essentially handle the re-orientation yourself.
However now my app is crashing when I rotate the device.
Testing with Android 4.1.2.
Anyone see anything off with my code? Sorry if the answer is obvious, but I've been stuck on this bug for a couple of days now. Maybe I just need another set of eyes on it. Thanks in advance!
Note: the line #SuppressWarnings("deprecation") is not in the example, it was suggested by the editor in order to compile.
The Main Activity:
package com.example.xxx.xxx;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
protected FrameLayout webViewPlaceholder;
protected WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
#SuppressWarnings("deprecation")
protected void initUI() {
webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));
if (myWebView == null){
//Create It
myWebView = new WebView(this);
myWebView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadsImagesAutomatically(true);
myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebView.setScrollbarFadingEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setInitialScale(1);
myWebView.loadUrl("http://www.theurl.com");
}
webViewPlaceholder.addView(myWebView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onConfigurationChanged(Configuration newConfig){
if (myWebView != null) {
webViewPlaceholder.removeView(myWebView);
}
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
initUI();
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
myWebView.restoreState(savedInstanceState);
}
}
Main Activity XML:
<LinearLayout 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:orientation="horizontal" >
<FrameLayout android:id="#+id/webViewPlaceholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
try adding orientation to you activity definition in manifest file as follows:
<activity android:screenOrientation="portrait" ...>
</activity>
I realized I don't even need WebChromeClient at all if I'm not using any of it's features. Found answer here: How to prevent WebView auto refresh when screen rotation
simply add "screenSize" to your configchanges in manifest.
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

Android Webview scroll is misbehaving when loading a flash website

I am opening a simple flash based website in my webview using following code:
Java Code:
package com.sample.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class SampleWebViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webView = (WebView) findViewById(R.id.webview);
final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("loading");
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progress.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.dismiss();
}
});
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("http://www.fusioncharts.com/demos/features/#linkedcharts-for-easy-drill-down");
}
});
}
}
Main.xml:
<?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" >
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Load URL" />
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp" />
</LinearLayout>
Now when the page is loaded completely and i am trying to scroll down to view rest of the content, the webview is coming on top of "Load URL" button. Check the screenshot:
while it should look like:
Please suggest, what should i do so that it works correctly.
Edit: I am using a Galaxy Note(OSv2.3.5) with the latest version of both Flash Player and Adobe Air installed.
I have also tried this things but I think the only solution to do this is by using only a single webview in your xml, so, you don't have other native controls.
This is really broken on Android. If you try to use the Build in topbar it will also go behind the flash. I'd really recommend you go away (far away) from flash. There is some discussions about it on google groups and some on stackoverflow but all ends without any good results. The only good suggestion I found was using flash to cover other flash components =/

Intent not working, crashes the application

I am building this app and I can't make it work. Here is the code and if you find the problem please post the solution.
This is the Main Activity
package in.isuru.caf;
//imports imported here. removed to simplify the code.
public class MainList extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String[] main_items_array = getResources().getStringArray(R.array.main_items);
setListAdapter(new ArrayAdapter<String>(MainList.this, android.R.layout.simple_list_item_1, main_items_array));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
String selectedFromList = (String) (lv.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), selectedFromList, Toast.LENGTH_SHORT).show();
if(selectedFromList.contains("Top 20 Questions")){
Intent mainIntent = new Intent(MainList.this, in.isuru.caf.Top20Questions.class);
startActivity(mainIntent);
}
}
});
}
}
This is the second activity.
package in.isuru.caf;
import in.isuru.caf.R;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Top20Questions extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.forums.catholic.com/forumdisplay.php?f=4&daysprune=-1&order=desc&sort=views");
setContentView(R.layout.top_20_questions);
}
}
This is the AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.isuru.caf"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MainList" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Top20Questions"></activity>
</application>
</manifest>
And I am getting this error.
01-21 12:18:44.231: E/AndroidRuntime(1767):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{in.isuru.caf/in.isuru.caf.Top20Questions}:
java.lang.NullPointerException
You must call the setContentView() method before trying to access any resources.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top_20_questions);
WebView myWebView = (WebView) findViewById(R.id.webview);
...
...
}
Try this:
Intent mainIntent = new Intent(MainList.this, Top20Questions.class);
startActivity(mainIntent);
The problem is in the order of the instructions:
setContentView(R.layout.top_20_questions);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("...");
You have to set the content view first, then look for a view with a given id.
you need to fix your second activity.
see below:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.top_20_questions);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.forums.catholic.com/forumdisplay.php?f=4&daysprune=-1&order=desc&sort=views");
}
you need to call setContentView(..) before trying find any views. So put your setContentView(..) just after super.onCreate(..) in your second activity and it should work fine.
You forgot the following line on your Top20Questions class:
setContentView(R.layout.your_layout);
First Set content view by using setContentView then try to use IDs from it . then only above code will work
you need to setContentView(R.layout.top_20_questions);

Categories

Resources