I want users of my app to click on an image, if they do so, a new activity must be loaded. This activity then shows a predetermined webpage. But when adding the webviewer widget, it doesn't work.
Activity name = activity_webbrowser_inno.xml
Actual webviewer widget name = webview_inno (in a constraint layout)
Java name = activity_webbrowser_inno
In the XML file:
<WebView
android:id="#+id/webview_inno"
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" />
In the activity_webbrowser_inno.java file:
package eu.inno.inno
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
public class activity_webbrowser_inno extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webbrowser_inno);
}
WebView webview_inno = (WebView) findViewById(R.id.webview_inno);
webview_inno.loadUrl("http://www.inno.eu")
ERROR in red on the loadUrl. On hover: cannot resolve symbol 'loadUrl'.
I know there are other threads about this, but following their steps didn't seem to work. Or I couldn't understand them. This is my first project.
private void uiBind() {
webView = (WebView) findViewById(R.id.web_view);
webView.setWebViewClient(new FileWebView());
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
if (mode == Constant.PDF_FILE) {
webView.loadUrl("http://docs.google.com/gview?embedded=true&url="
+ "http://www.5gamericas.org/files/2714/1471/2645/4G_Americas_Recommendations_on_5G_Requirements_and_Solutions_10_14_2014-FINALx.pdf");
} else if (mode == Constant.DOC_FILE) {
webView.loadUrl("http://docs.google.com/gview?embedded=true&url="
+ "http://ijsetr.com/uploads/4256135G%20Mobile%20Wireless%20Technology.docx");
} else if (mode == Constant.EXCEL_FILE) {
webView.loadUrl("http://docs.google.com/gview?embedded=true&url="
+ "http://download.microsoft.com/download/1/4/E/14EDED28-6C58-4055-A65C-23B4DA81C4DE/Financial%20Sample.xlsx");
}
}
public class FileWebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
Related
I'm using Android Webview to display a web page inside my application, I'm using Java. I'm not a very experienced programmer. My website is made with WordPress. In my Android App I would like to programmatically remove Header and Footer of all pages displayed in my app through the Android Webview.
If I check the ID of the header in my website, the ID is: #meanmenu
So I can remove it with CSS like so:
#meanmenu {
display: none;
}
The Footer does not have an ID or Class but I can remove it with CSS like below:
footer {
display: none;
}
How can I remove both the Header and Footer automatically when the pages load in my Webview?
Below is part of my Java code for the Webview:
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class RentalsActivity extends AppCompatActivity {
/** Called when the activity is first created. */
WebView web;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rentals);
web = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://www.sample.com/pages");
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Every time I swipe to refresh, my app reloads to the homepage instead of reloading the same web-page. I tried every way to detect this behaviour but couldn't find out anything. Can you tell what's the issue here?
Swipe to refresh should only be resfreshing the current web page instead of the whole app right?
MainActivity.java
package com.yoalfaaz.yoalfaaz;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.support.v4.view.MenuItemCompat;
public class MainActivity extends AppCompatActivity {
private WebView YoWeb;
private ShareActionProvider mShareActionProvider;
SwipeRefreshLayout swipe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb() {
YoWeb = (WebView)findViewById(R.id.webview);
WebSettings webSettings = YoWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
YoWeb.loadUrl("https://www.yoalfaaz.com");
swipe.setRefreshing(true);
YoWeb.setWebViewClient(new WebViewClient() {
//onPageFinished Method
public void onPageFinished(WebView view, String url) {
//Hide the SwipeRefreshLayout
swipe.setRefreshing(false);
}
});
}
#Override
public void onBackPressed() {
if (YoWeb.canGoBack()) {
YoWeb.goBack();
} else {
super.onBackPressed();
}
}
#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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
//private Intent setShareIntent() {
// Intent shareIntent = new Intent();
// shareIntent.setAction(Intent.ACTION_SEND);
// shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
// shareIntent.setType("text/plain");
// startActivity(shareIntent);
// return shareIntent;
//}
}
content_main.xml
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.yoalfaaz.yoalfaaz.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swiperefresh"
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"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
These are the two files responsible for swipe to refresh and you can see all the code that is there. I hope that you might be able to figure out exactly what's the issue here.
Your SwipeRefreshLayout.OnRefreshListener is calling LoadWeb method. Inside LoadWeb you are calling YoWeb.loadUrl("https://www.yoalfaaz.com"); which means you are always setting the webview to go to that URL on refresh. Change your code to be like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
YoWeb = (WebView)findViewById(R.id.webview); // Move your declaration up here
swipe = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb(YoWeb.getUrl()); // Pass in the current url to refresh
}
});
LoadWeb("https://www.yoalfaaz.com"); // load the home page only once
}
public void LoadWeb(String url) // Pass in URL you want to load
{
WebSettings webSettings = YoWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
YoWeb.loadUrl(url); // Load the URL passed into the method
swipe.setRefreshing(true);
YoWeb.setWebViewClient(new WebViewClient() {
//onPageFinished Method
public void onPageFinished(WebView view, String url) {
//Hide the SwipeRefreshLayout
swipe.setRefreshing(false);
}
});
}
I have problem.
I do a progressbar in main2activity.
App starts but when the website its loaded the progressbar is not disappear.
And my app crashed.
Please help ! :*
I would like to correct my code because this is the best way to learn where was a mistake.
Thanks!
There is code :
package musial.pzstis.com.pzstiz;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
/*
* Demo of creating an application to open any URL inside the application and clicking on any link from that URl
should not open Native browser but that URL should open in the same screen.
- Load WebView with progress bar
*/
public class Main2Activity extends Activity {
/** Called when the activity is first created. */
WebView myWebView;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
myWebView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar7);
myWebView.setWebViewClient(new myWebClient());
myWebView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = myWebView.getSettings();
myWebView.loadUrl("https://docs.google.com/viewer?url=http%3A%2F%2Fpzstiz.swiebodzin.pl%2Fzastepstwa%2FPZSTiZ_zastepstwa.pdf");
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
There are LOGS:
02-14 20:43:38.592 20262-20299/musial.pzstis.com.pzstiz E/EGL_emulation: tid 20299: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-14 20:43:44.672 20262-20262/musial.pzstis.com.pzstiz E/AndroidRuntime: FATAL EXCEPTION: main
Process: musial.pzstis.com.pzstiz, PID: 20262
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at musial.pzstis.com.pzstiz.Main2Activity$myWebClient.onPageFinished(Main2Activity.java:66)
at com.android.webview.chromium.WebViewContentsClientAdapter.onPageFinished(WebViewContentsClientAdapter.java:531)
at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:188)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
02-14 20:43:44.777 1569-4538/system_process E/EGL_emulation: tid 4538: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
Pass progressBar to your custom webview :
myWebView.setWebViewClient(new myWebClient(progressBar));
Change your myWebClient class to this:
public class myWebClient extends WebViewClient {
private ProgressBar progressBar;
public myWebClient(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
Hope this helps.
Well, you got an null pointer Exception with the progressbar, i would encapsulate every access to the progressbar in this form:
if(progressBar == null)
progressBar = (ProgressBar) findViewById(R.id.progressBar7);
if(progressBar)
progressBar.setVisibility(View.GONE);
Implemented in vour code:
public class Main2Activity extends Activity {
/** Called when the activity is first created. */
WebView myWebView;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
myWebView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar7);
myWebView.setWebViewClient(new myWebClient(this));
myWebView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = myWebView.getSettings();
myWebView.loadUrl("https://docs.google.com/viewer?url=http%3A%2F%2Fpzstiz.swiebodzin.pl%2Fzastepstwa%2FPZSTiZ_zastepstwa.pdf");
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
}
public class myWebClient extends WebViewClient
{
privat Activity main2ac;
ProgressBar progressBar;
public myWebClient(Activity handle){
main2ac = handle;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if(progressBar == null)
progressBar = (ProgressBar)main2ac.findViewById(R.id.progressBar7);
if(progressBar)
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(progressBar == null)
progressBar = (ProgressBar)main2ac.findViewById(R.id.progressBar7);
if(progressBar)
progressBar.setVisibility(View.GONE);
}
}
How show Banner Ads on Newspaper Link "Webview" Android Eclipse
I already set Admob "Banner Ads and Interstitial Ads" on my Project. Interstitial Ads Work Correctly but Banner Ads not Show on Webview.
Here my Java & xml Code
Details_Newspaper.Java
package com.nasir.newspaper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
#SuppressLint("SetJavaScriptEnabled")
public class Details_Newapaper extends Activity {
WebView mWebview;
AdView mAdView;
private InterstitialAd interAd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.details_newapaper );
// For Banner Ads View
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// For Interstitial Ads View
interAd = new InterstitialAd(this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest_FC = new AdRequest.Builder().build();
interAd.loadAd(adRequest_FC);
interAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
displayInterstitial();
}
});
getOverflowMenu();
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setSupportZoom(true); //Zoom Control on web (You don't need this
//if ROM supports Multi-Touch
mWebview.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
final Activity Activity = this;
mWebview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
Activity.setTitle("Loading..... Please Wait");
Activity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
Activity.setTitle(R.string.app_name);
}
});
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(Activity, description, Toast.LENGTH_SHORT).show();
}
});
String customHtml = getIntent().getStringExtra("Newspaper_Name");
mWebview.loadUrl(customHtml);
setContentView(mWebview);
mWebview.getSettings().setBuiltInZoomControls(true);
}
// On Back Button Press, Go to Website Back
#Override
public void onBackPressed() {
if (mWebview.isFocused() && mWebview.canGoBack()) {
mWebview.goBack();
} else {
super.onBackPressed();
finish();
}
}
// For Interstitial Ads View
public void displayInterstitial() {
if (interAd.isLoaded()) {
interAd.show();
}
}
/** Called when leaving the activity */
#Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
#Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
/** Called before the activity is destroyed */
#Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
// inflate for action bar
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.refresh_main, menu);
return true;
}
// handle click events for action bar items
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
showToast("Refresh was clicked.");
return true;
// case R.id.share:
// showToast("Share was clicked.");
// return true;
//
default:
return super.onOptionsItemSelected(item);
}
}
// put the other two menu on the three dots (overflow)
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
java.lang.reflect.Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// so that we know something was triggered
public void showToast(String msg){
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
details_newspaper.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|fill_vertical"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<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="BANNER"
ads:adUnitId="ca-app-pub-1528948212400372/9325301249" >
</com.google.android.gms.ads.AdView>
</RelativeLayout>
I've searched everywhere, and nothing found!
I need to set the position of my progressDialog in the center horizontal and vertical.
But when i compile my code by command line (i dont use eclipse) an error message is shown:
"cannot find symbol > Gravity..".
I think its a problem in the inclusion, i've imported looot of useless file 'cause i dont know what to import.
Somebody could help me?
Here is the code:
package com.worfut.project;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.ContextThemeWrapper;
import android.widget.ShareActionProvider;
import android.widget.LinearLayout;
import android.view.Window;
public class MainActivity extends Activity {
WebView WorfutWeb;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
final Activity activity = this;
WorfutWeb = (WebView) findViewById(R.id.mainWebView);
WorfutWeb.getSettings().setSupportZoom(false);
WorfutWeb.getSettings().setBuiltInZoomControls(false);
WorfutWeb.setBackgroundColor(Color.BLACK);
WorfutWeb.setWebViewClient(new myWebClient());
WorfutWeb.getSettings().setJavaScriptEnabled(true);
// prepare for a progress bar dialog
progressBar = new ProgressDialog(this, R.id.mainWebView);
progressBar.setCancelable(true);
progressBar.setIndeterminate(false);
progressBar.getWindow().setGravity(Gravity.CENTER);
progressBar.setMessage("Caricamento in corso..");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
WorfutWeb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, final int progress)
{
activity.setProgress(progress * 100);
progressBar.setProgress(progress);
if(progress == 100)
{
progressBar.dismiss();
}
}
});
WorfutWeb.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
String html1 = "<html><body><br>";
String html2 = "<div style='background: #007ac1; border: 2px solid lightblue; border-radius: 8px; padding: 4px;'>";
String html3 = "<p style='font-weight: bolder;color: white;'><h2><u>Si รจ verificato un errore!</u></h3><br>";
String html4 = "Possibili cause:<br><ul><li>";
String html5 = "Non si dispone di una connessione Internet;</li><ul><li>Chiudere l'applicazione</li><li>Attivare la connessione Internet</li><li>Riavviare l'applicazione</li></ul><li>L'applicazione non risponde<ul><li>Chiudere e riaprire l'applicazione</li><li>Scriveteci sul Market Android</li></ul>";
String html6 = "</li></ul></p><br><p align=right><font color=white><em>Worfut Staff.</em> </font></p></div></body></html>";
String mime = "text/html";
String encoding = "utf-8";
view.loadData(html1+html2+html3+html4+html5+html6, mime, encoding);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
WorfutWeb.startAnimation(fadeInAnimation);
WorfutWeb.loadUrl("http://www.americantrip.altervista.org/try");
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
WorfutWeb.loadUrl("http://americantrip.altervista.org/try/info.php");
// inside the menu button you don't need to call Super.loadUrl, LoadUrl its enough
return true;
case R.id.menu_help:
WorfutWeb.loadUrl("http://americantrip.altervista.org/try/help.php");
// inside the menu button you don't need to call Super.loadUrl, LoadUrl its enough
return true;
case R.id.menu_search:
WorfutWeb.loadUrl("http://americantrip.altervista.org/try/cerca.php");
return true;
case R.id.menu_exit:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
public void onResume() {
super.onResume();
if (WorfutWeb != null) {
WorfutWeb.resumeTimers();
}
}
public void onPause() {
super.onPause();
if (WorfutWeb != null) {
WorfutWeb.pauseTimers();
}
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.dismiss();
}
}
// To handle "Back" kewy press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && WorfutWeb.canGoBack()) {
WorfutWeb.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
This is the layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/sfondo"
android:gravity="center">
>
<WebView android:id="#+id/mainWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
The part with the error is this:
progressBar.getWindow().setGravity(Gravity.CENTER);
and i think its correct, but maybe i have to import something else.
Thanks yall.
P.S. i've already seen http://developer.android.com/reference/android/widget/LinearLayout.html#setGravity(int)
and i've took from there the "import android.widget.LinearLayout". seems to be the same, dont run.
Try:
import android.view.Gravity;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/sfondo"
>
>
<WebView android:id="#+id/mainWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
modified your layout, i guess the progress wont be at the center as you declared your layout on the center. anyway if that doesn't help you.
the easiest way to do it is.
FrameLayout.LayoutParams para=new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT );
para.gravity = Gravity.CENTER;
progressBar.setLayoutParams(para);
and it should work. if that fixed your issue please do accept the answer so others can get use of it. cheers