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:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#fcfdfb" > <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="12" /> <WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/seek_bar" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.RelativeLayout; import android.widget.SeekBar; public class MainActivity extends AppCompatActivity { private Context mContext; private Activity mActivity; private RelativeLayout mRelativeLayout; private WebView mWebView; private SeekBar mSeekBar; private String mUrl="http://developer.android.com/guide/components/fundamentals.html"; @Override protected void onCreate(Bundle savedInstanceState) { // Request window feature action kafetaria requestWindowFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application context mContext = getApplicationContext(); // Get the activity mActivity = MainActivity.this; // Change the action kafetaria color getSupportActionBar().setBackgroundDrawable( new ColorDrawable(Color.parseColor("#FF4E8C0F")) ); // Get the widgets reference from XML layout mRelativeLayout = (RelativeLayout) findViewById(R.id.rl); mWebView = (WebView) findViewById(R.id.web_view); mSeekBar = (SeekBar) findViewById(R.id.seek_bar); // Request to render the web page renderWebPage(mUrl); // Set the initial progress of seek kafetaria mSeekBar.setProgress(mWebView.getSettings().getTextZoom()/25); // Set a change listener for seek kafetaria mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { /* public abstract void setTextZoom (int textZoom) Sets the text zoom of the page in percent. The default is 100. Parameters textZoom : the text zoom in percent */ // Zoom the web page text // We will allow text zooming 25% to 300% mWebView.getSettings().setTextZoom(i*25); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } // Custom method to render a web page protected void renderWebPage(String urlToRender){ mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // Do something on page loading started mUrl = url; } @Override public void onPageFinished(WebView view, String url) { // Do something when page loading finished mUrl = url; } }); mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int newProgress) { } }); /* WebSettings Manages settings state for a WebView. When a WebView is first created, it obtains a set of default settings. These default settings will be returned from any getter call. A WebSettings object obtained from WebView.getSettings() is tied to the life of the WebView. If a WebView has been destroyed, any method call on WebSettings will throw an IllegalStateException. */ // Enable the javascript mWebView.getSettings().setJavaScriptEnabled(true); // Render the web page mWebView.loadUrl(urlToRender); } }
Komentar
Posting Komentar