WebView causes application restart - Fatal signal 11 (SIGSEGV) - android

I have an activity that displays web pages. On some phones (e.g, Huawei Ascend) , this activity crashes in a way that causes the application to restart, but doesn't cause the usual android error popup for reporting. (Other phones (e.g. Nexus) never crash at all.) It does appear to always crash while a web page is loading, but not at any particular point e.g. sometimes at shoulOverrideUrlLoading, sometimes during onPageStarted or onPageFinished.
I think the WebView is causing this--
08-29 21:08:22.577: A/libc(957): Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 957
--but I can't work out why.
Here's the activity:
public class WebDisplay extends Activity
{
private String sentUrl = "";
public WebView myWebView;
public ProgressBar spinner;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d("WEBVIEW", "onCreate");
getWindow().setBackgroundDrawableResource(R.color.white);
Bundle extras = getIntent().getExtras();
if (null != extras)
{
sentUrl = extras.getString("displayURL");
}
setContentView(R.layout.webdisplay);
myWebView = (WebView) findViewById(R.id.webDisplay);
spinner = (ProgressBar) findViewById(R.id.webLoading);
// WebView displays default to showing things in exact pixel size at 100%
// resolution, meaning it generally opens on a zoomed in portion of the
// top left of the web page and refuses to let you zoom out, so we have
// to force it to display pages a bit more reasonably.
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
// By default, there is no javascript support or zoom controls, so
// turn both of those on.
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// We want links to continue opening inside the app.
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBVIEW", "loadUrl="+url);
view.loadUrl(url); // open link in the same web view.
return true;
}
#Override
public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
Log.d("WEBVIEW", "onPageStarted="+url);
super.onPageStarted(view, url, favicon);
if (View.GONE == spinner.getVisibility()) { spinner.setVisibility(View.VISIBLE); }
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBVIEW", "onPageFinished="+url);
super.onPageFinished(view, url);
if (View.VISIBLE == spinner.getVisibility()) { spinner.setVisibility(View.GONE); }
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String url) {
Log.d("WEBVIEW", "onPageError="+url+"::descr="+description);
}
});
// Try to get YouTube videos working.
myWebView.setWebChromeClient(new WebChromeClient() {
});
// So the user doesn't have to back through their entire history to get
// back to the app, we provide a "done browsing" button.
Button doneButton = (Button) findViewById(R.id.doneButton);
doneButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
finish();
}
});
ImageButton backButton = (ImageButton) findViewById(R.id.webBackButton);
backButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
myWebView.goBack();
}
});
ImageButton fwdButton = (ImageButton) findViewById(R.id.webForwardButton);
fwdButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
myWebView.goForward();
}
});
ImageButton refreshButton = (ImageButton) findViewById(R.id.refreshButton);
refreshButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
myWebView.reload();
}
});
myWebView.loadUrl(sentUrl);
}
#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);
}
}
And here's the loaded layout:
<?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"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/webNavBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/abs__background_holo_light"
android:layout_alignParentBottom="true"
android:padding="2dp" >
<Button
android:id="#+id/doneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/refreshButton"
android:layout_alignTop="#+id/refreshButton"
android:background="#drawable/custom_button"
android:padding="5dp"
android:text="#string/web_done"
android:textColor="#color/white"
android:textStyle="bold" />
<ImageButton
android:id="#+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/custom_button"
android:src="#drawable/ic_action_refresh"
android:contentDescription="#string/web_refresh"
android:textColor="#color/white"
android:padding="5dp" />
<ImageButton
android:id="#+id/webBackButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/webForwardButton"
android:background="#drawable/custom_button"
android:src="#drawable/navigation_back"
android:padding="5dp"
android:contentDescription="#string/web_back"
android:textColor="#color/white" />
<ImageButton
android:id="#+id/webForwardButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/custom_button"
android:src="#drawable/navigation_forward"
android:contentDescription="#string/web_forward"
android:textColor="#color/white"
android:padding="5dp" />
</RelativeLayout>
<WebView
android:id="#+id/webDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/webNavBar" />
<ProgressBar
android:id="#+id/webLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="80dp"
style="#android:style/Widget.ProgressBar.Large"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ETA: Turning hardware acceleration off like this
myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
seems to stop the SIGSEV thing, but also stops YouTube videos playing (I still have audio, but no image).

make sure the WebView is the first element of the RelativeLayout.
or don't use RelativeLayout, just try LinearLayout.
or remove the attributes
android:layout_alignParentTop="true"
android:layout_above="#id/webNavBar"
of WebView

webSettings.setJavaScriptEnabled(true);
Settings above causes "Fatal signal 11 (SIGSEGV)" error for some sites using java script. I reproduced this error on Samsung Galaxy Tab 3 And Galaxy tab 3 lite. Android version is 4.4.2 for both devices.
If I remove "setJavaScriptEnabled" function, it is working fine.

Related

Playing new videos with Youtube Api on Android

I'm developing an app with Android Studio for which I need the Youtube API. The problem is that I want the video to be played
every time the user clicks on "Reproducir"("play"). So I have researched how to load a video and everybody says to use the function
initializates that will call onInitializationSuccess and playes there. But if I want to load new videos, parsing the url and playing the new videos I can't. I tried calling the initialization everytime the user clicks on Reproducir but in that case It ig.
public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_video);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
urlTxtView = (EditText) findViewById(R.id.urlYoutube);
playButton = (Button) findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
String url = urlTxtView.getText().toString();
Log.d("1",url );
youTubePlayer.loadVideo("a4NT5iBFuZs");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "ERROR:"+result.toString(), Toast.LENGTH_LONG).show();
}
XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/action_add_video"
android:textStyle="normal|bold"
android:textSize="30sp"
android:gravity="center_horizontal"
android:layout_marginBottom="20dp" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"/>
<EditText
android:id="#+id/tituloVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Titulo"
android:inputType="textPersonName" />
<EditText
android:id="#+id/urlYoutube"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="URL o buscar con #youtube"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/buttonCancelar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancelar" />
<Button
android:id="#+id/buttonPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reproducir" />
<Button
android:id="#+id/buttonGuardar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Guardar" />
</LinearLayout>
</LinearLayout>
Log:
W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.view.View{a21cb6f V.ED.... ........ 0,0-768,50 #102002f android:id/statusBarBackground}. Bottom edge 36 px below YouTubePlayerView's top edge. .
Here there is an image of the idea:
Picture of Android emulator, the user should introduce an url, clicks on Reproducir and plays the video from the url
If you are reinitilize the youtube video video view if already its been initialize thn first you need to release exising youtube view.
Please check below code
Specifically i've changed initialize part on Play button click
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (youTubePlayer != null) {
youTubePlayer.release();
}
youTubeView.initialize("AIzaSyDo9p_Qjy8XRBdFnVOaIx586MzHHLOUopw", (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
Entire Activity code
public class AddVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayerView youTubeView;
private EditText urlTxtView;
private Button playButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_video);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_player_add);
urlTxtView = (EditText) findViewById(R.id.urlYoutube);
playButton = (Button) findViewById(R.id.buttonPlay);
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (youTubePlayer != null) {
youTubePlayer.release();
}
youTubeView.initialize(Config.YOUTUBE_API_KEY, (YouTubePlayer.OnInitializedListener) view.getContext());
}
});
}
private YouTubePlayer youTubePlayer;
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
this.youTubePlayer = youTubePlayer;
String url = urlTxtView.getText().toString();
Log.d("1", url);
if (!wasRestored) {/// here videoid you need to pass not entire video URL
youTubePlayer.loadVideo(url);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "ERROR:" + result.toString(), Toast.LENGTH_LONG).show();
}
}
to load video you need to pass video id not video url.So with assumption tht in edit text video id will be pass or if url thn you will get the video id from url and thn video will be loaded
this.youTubePlayer = youTubePlayer;
String url = urlTxtView.getText().toString();
Log.d("1", url);
if (!wasRestored) {/// here videoid you need to pass not entire video URL
youTubePlayer.loadVideo(url);
}
And for below log
Log: W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to
unauthorized overlay on top of player. The YouTubePlayerView is
obscured by android.view.View{a21cb6f V.ED.... ........ 0,0-768,50
102002f android:id/statusBarBackground}. Bottom edge 36 px below YouTubePlayerView's top edge.
you can refer answer https://stackoverflow.com/a/29676512/1140237
in your case for this log... issue is with Application or Activity theme. Give No ActionBarTheme to resolve that error

Keep current page rendered until the next page is loaded

I have a simple app that is based on WebView.loadUrl("http://www.example.com").
When a user clicks on a URL, the default behavior is to immediately show a blank page, wait until the page is loaded, and then show this page.
I managed to show a splash screen in lieu of the blank page. However, is it possible to keep the current page rendered instead, and render the next page only when it is fully loaded?
This is, I feel, a hacky solution. I offer it as a temporary fix until someone posts the "real" way to do this.
MainActivity class:
public class MainActivity extends Activity
{
WebView webView1, webView2;
EditText editText;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.edit_url);
webView1 = (WebView) findViewById(R.id.webView_1);
webView2 = (WebView) findViewById(R.id.webView_2);
webView1.setWebViewClient(client);
webView2.setWebViewClient(client);
webView1.loadUrl(editText.getText().toString());
}
private WebViewClient client = new WebViewClient()
{
public void onPageFinished(WebView webView, String url)
{
if (webView == webView1)
webView2.setVisibility(View.GONE);
else
webView1.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "WebView " + (webView == webView1 ? "One" : "Two") + " is showing", 0).show();
}
};
private void loadUrl(String url)
{
if (webView1.getVisibility() == View.GONE)
webView1.loadUrl(url);
else
webView2.loadUrl(url);
}
public void onClick(View v)
{
loadUrl(editText.getText().toString());
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.webkit.WebView android:id="#+id/webView_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.webkit.WebView android:id="#+id/webView_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText android:id="#+id/edit_url"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:layout_gravity="center_vertical"
android:text="http://www.example.com" />
<Button android:id="#+id/button_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Load" />
</LinearLayout>
</LinearLayout>

website is not opening in webview

I am trying to open a url in my webview but the problem is that the url is not loading in my webview, its showing pop-up for the selection of browser and as soon as i click on an option it opens the url in that browser. Can somebody help me to solve this, i want the url to open in the webview itself not in the browser.
my xml for webview is :-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_new" >
<RelativeLayout
android:id="#+id/upBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/wbview_bckbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back_btn_chng" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/upBar" >
<WebView
android:id="#+id/linkwebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:scrollbars="none" />
</RelativeLayout>
</RelativeLayout>
and here's my code for webview :-
public class FbTwtrLink extends Activity {
WebView web;
ImageView img,bckimg,share_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fb_twtr_link);
img = (ImageView)findViewById(R.id.header);
bckimg = (ImageView) findViewById(R.id.wbview_bckbtn);
web=(WebView)findViewById(R.id.linkwebview);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_fb_twtr_link, menu);
return true;
}
}
and i m calling the webview like that :-
fbicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(context,FbTwtrLink.class);
//intent.putExtra("fb_link", 1);
startActivity(intent);
}
});
You should override WebViewClient and its method shouldOverrideUrlLoading
web.setWebViewClient(new MyWebClient());
web.loadUrl("http://www.google.com");
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//1 option
// getApplicationContext().startActivity(
// new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// 2 option
//view.loadUrl(url);
return true;
}
}
Add this line may help:
webView.getSettings().setPluginState(PluginState.ON);
also add internet permission in your manifest
<uses-permission android:name="android.permission.INTERNET" />
Also if using API lever greater equal to 11 then you can use this in you manifest at application level
android:hardwareAccelerated="true"
You can add the below statement to open in webview:
mweb.setWebViewClient(new WebViewClient());
You can use Direct this >
fbicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent (android.content.Intent.ACTION_VIEW, Uri.parse ("http://www,google.com")); // Prepare intent
startActivity(intent);
}
});

How to get buttons to display above WebView

I am aware that this question has been asked before, but I have tried every "solution" to the other questions, but I still have not been able to achieve what I am trying to do. I have searched and searched, and tried and tried. Please forgive me. I simply want to display my row of buttons above my WebView, so that it is consistent with the rest of the activities in my app (they all have the row of buttons at the top). It seems like I have tried every possible combination of layouts, but to no avail. It displays correctly in the Graphical Layout (Eclipse), but on the phone and emulator it only displays the WebView, fullscreen, no buttons.... Any help will be very much appreciated!
Here's my 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" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/menu_panel">
<ImageButton
android:id="#+id/bHomeMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_home"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="Home" />
<ImageButton
android:id="#+id/bItsMe247Menu"
android:layout_toRightOf="#+id/bHomeMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_selected_hcu_light_blue"
android:src="#drawable/icon_menu_login"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="It'sMe" />
<ImageButton
android:id="#+id/bFindUsMenu"
android:layout_toRightOf="#+id/bItsMe247Menu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_find_us"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="FindUs" />
<ImageButton
android:id="#+id/bEmailMenu"
android:layout_toRightOf="#+id/bFindUsMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_email"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="Email" />
<ImageButton
android:id="#+id/bRatesMenu"
android:layout_toRightOf="#+id/bEmailMenu"
android:layout_margin="-8dip"
android:layout_width="65dp"
android:layout_height="90dp"
android:background="#drawable/menu_btn_hcu_light_blue"
android:src="#drawable/icon_menu_rates"
android:adjustViewBounds="true"
android:scaleType="centerInside"
style="#style/ButtonText"
android:textSize="13sp"
android:text="Rates" />
</LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wvItsMe"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
EDIT: Here's my java code, and a note below it:
package com.dummyapp.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.dummyapp.app.SimpleGestureFilter.SimpleGestureListener;
public class ItsMe247 extends Activity implements SimpleGestureListener{
private SimpleGestureFilter detector;
private WebView itsMeLogin;
// ImageButton menuHome, menuFindUs, menuEmail, menuRates;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.itsme247);
// menuHome = (ImageButton)findViewById(R.id.bHomeMenu);
// menuFindUs = (ImageButton)findViewById(R.id.bFindUsMenu);
// menuEmail = (ImageButton)findViewById(R.id.bEmailMenu);
// menuRates = (ImageButton)findViewById(R.id.bRatesMenu);
detector = new SimpleGestureFilter(this,this);
itsMeLogin = new WebView(this);
Toast.makeText(this, "Just Swipe To Exit", Toast.LENGTH_LONG).show();
itsMeLogin.getSettings().setJavaScriptEnabled(true); // JavaScript enabled
getWindow().requestFeature(Window.FEATURE_PROGRESS); // Show progress bar of page loading
final Activity itsMeActivity = this;
itsMeLogin.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
itsMeActivity.setProgress(progress * 100);
}
});
itsMeLogin.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(itsMeActivity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
itsMeLogin.loadUrl("http://www.google.com");
setContentView(itsMeLogin);
// itsMeLogin.setWebViewClient(new WebViewClient() {
// #Override
// public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
// return super.shouldOverrideUrlLoading(view, url);
// }
// });
//******************* MENU BUTTONS START HERE ********************************************************//
// menuRates.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent ratesIntent = new Intent(ItsMe247.this, Rates.class);
// ItsMe247.this.startActivity(ratesIntent);
//
// }
//
// });
//
//
// menuFindUs.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent contactIntent = new Intent(ItsMe247.this, Contact.class);
// ItsMe247.this.startActivity(contactIntent);
//
// }
// });
//
// menuEmail.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent emailIntent = new Intent(ItsMe247.this, Email.class);
// ItsMe247.this.startActivity(emailIntent);
//
//
// }
// });
//
// menuHome.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Intent itsMeIntent = new Intent(ItsMe247.this, MainActivity.class);
// ItsMe247.this.startActivity(itsMeIntent);
// }
// });
//***************************** MENU BUTTONS END HERE ********************************************************//
}//end onCreate method
#Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
public void onSwipe(int direction) {
switch (direction) {
case SimpleGestureFilter.SWIPE_RIGHT:
Intent funIntent = new Intent(ItsMe247.this, MainActivity.class);
ItsMe247.this.startActivity(funIntent);
//ItsMe247.this.finish();
break;
case SimpleGestureFilter.SWIPE_LEFT:
Intent funIntent2 = new Intent(ItsMe247.this, Contact.class);
ItsMe247.this.startActivity(funIntent2);
//ItsMe247.this.finish();
break;
case SimpleGestureFilter.SWIPE_DOWN:
break;
case SimpleGestureFilter.SWIPE_UP:
break;
}
}//end onSwipe method
public void onDoubleTap() {
}
}
I have all the menu buttons commented out (the buttons I'm trying to get to display above webview) until I finally get them to display through the xml. However, whenever I UNcomment the buttons and their listeners, the app crashes when trying to enter this activity.
Your code isn't using your XML layout, you're creating your own WebView in code and setting that as the view so it bypasses your XML completely.
I've provided a few changes here that should make it work. Pay particular attention to the setContentView line and the following line, these are what use the XML layout. You may need to change the name of the layout (R.layout.whatever) and the webview id (R.id.webview). There may be a few parse errors from me putting it here, might have missed a closing bracket. I deleted much of your commented out code just to keep it simple.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.xmlLayout );
itsMeLogin = (WebView)findViewById( R.id.webview );
detector = new SimpleGestureFilter(this,this);
Toast.makeText(this, "Just Swipe To Exit", Toast.LENGTH_LONG).show();
itsMeLogin.getSettings().setJavaScriptEnabled(true); // JavaScript enabled
getWindow().requestFeature(Window.FEATURE_PROGRESS); // Show progress bar of page loading
final Activity itsMeActivity = this;
itsMeLogin.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
itsMeActivity.setProgress(progress * 100);
}
});
itsMeLogin.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(itsMeActivity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
itsMeLogin.loadUrl("http://www.google.com");
}
Here's my XML file from a similar layout as well, though yours will probably work with the edited code.
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:columnCount="3"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<EditText
android:ems="4"
android:imeOptions="actionNext"
android:inputType="number"
android:layout_gravity="center_horizontal"
android:maxLength="4"
android:selectAllOnFocus="true"
/>
<EditText
android:ems="4"
android:imeOptions="actionDone"
android:inputType="textCapCharacters|textNoSuggestions"
android:layout_gravity="center_horizontal"
android:maxLength="3"
android:selectAllOnFocus="true"
/>
<Button android:id="#+id/submit"
/>
<WebView android:id="#+id/webview"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_height="0dp"
android:layout_width="0dp"
/>
</GridLayout>

How do I get a progress dialog to show up when any link is clicked in a Webview

I have a header with some buttons in it and a webview below it. I have a working progress dialog for when the page originally opens and when a button is clicked. The problem is when I click any links inside of the webview a progress dialog doesn't show. Does anyone know the best method to make this happen? I included below the code for my WebViewClient and one of the buttons.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(myActivity.this, "","Loading...", true);
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
final Button btnCategory = (Button) findViewById(R.id.btnCategory);
btnCategory.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){
ProgressDialog dialog = ProgressDialog.show(myActivity.this, "","Loading...", true);
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
mWebView.loadUrl("http://mywebsite.com/page");
}
});
Instead of trying to show a dialog you could just toggle the visibility of a progress bar.
I haven't tried it myself but it could look like this:
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible" >
</ProgressBar>
you could use a relativelayout to include the webview and the progressbar (which is centered in the relativelayout).

Categories

Resources