۱۳۹۳ مهر ۳۰, چهارشنبه

WebView class

package ir.kiandroid.spacemag;





import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class ShowWebView extends Activity {

//private Button button;
private WebView webView;
@SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);

final Drawable dd = getResources().getDrawable(R.drawable.title);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(dd);
       
//Get webview
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default


if ( !isNetworkAvailable() ) { // loading offline
   webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
startWebView("http://spacemag.ir/");

}//end onCreate

private boolean isNetworkAvailable() {
   ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
   NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
   return activeNetworkInfo != null;
}

private void startWebView(String url) {
 
//Create new webview Client to show progress dialog
//When opening a url or click on link

webView.setWebViewClient(new WebViewClient() {    
       ProgressDialog progressDialog;
   
       //If you will not use this method url links are opeen in new brower not in webview
       public boolean shouldOverrideUrlLoading(WebView view, String url) {            
           view.loadUrl(url);
           return true;
       }
 
       //Show loader on url load
       public void onLoadResource (WebView view, String url) {
           if (progressDialog == null) {
               // in standard case YourActivity.this
               progressDialog = new ProgressDialog(ShowWebView.this);
               progressDialog.setMessage("مرزهای بیکران فضا...");
               progressDialog.show();
           }
       }
       public void onPageFinished(WebView view, String url) {
           try{
           if (progressDialog.isShowing()) {
               progressDialog.dismiss();
               progressDialog = null;
           }
           }catch(Exception exception){
               exception.printStackTrace();
           }
       }
     
   });
   
    // Javascript inabled on webview
   webView.getSettings().setJavaScriptEnabled(true);
 
   //webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
 
   // Other webview options
 
   webView.getSettings().setLoadWithOverviewMode(true);
   webView.getSettings().setUseWideViewPort(true);
   webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
   webView.setScrollbarFadingEnabled(false);
   webView.getSettings().setBuiltInZoomControls(true);
   webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
              Intent i = new Intent(Intent.ACTION_VIEW);
              i.setData(Uri.parse(url));
              startActivity(i);
            }
        });
 
 
   /*
    String summary = "You scored 192 points.";
         webview.loadData(summary, "text/html", null);
    */
 
   //Load url in webview
   webView.loadUrl(url);
   
   
}
///////////////////////////
// Open previous opened link from history on webview when back button pressed

@Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }
//////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.show_web_view, menu);
   return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
   // Handle item selection
   switch (item.getItemId()) {
       case R.id.aboutweb:
        startActivity(new Intent(ShowWebView.this, AboutWeb.class));
Toast.makeText(ShowWebView.this, "درباره مرزهای بیکران فضا", Toast.LENGTH_SHORT).show();
           return true;
         
       case R.id.aboutus:
        startActivity(new Intent(ShowWebView.this, AboutUS.class));
Toast.makeText(ShowWebView.this, "ارتباط با ما", Toast.LENGTH_SHORT).show();
           return true;
         
       case R.id.aboutdev:
        startActivity(new Intent(ShowWebView.this, AboutDev.class));
Toast.makeText(ShowWebView.this, "درباره توسعه دهنده", Toast.LENGTH_SHORT).show();
           return true;
     
       default:
           return super.onOptionsItemSelected(item);
   }
}
}

هیچ نظری موجود نیست: