WebView: how to preserve the user's zoom settings across sessions?

前端 未结 3 666
我在风中等你
我在风中等你 2020-12-10 08:45

My app uses a WebView, and users sometimes adjust the zoom level to make the text larger. However the zoom level setting is lost when the Activity is closed and another one

相关标签:
3条回答
  • 2020-12-10 09:29

    The answer Jorge gave is spot on, except that SaveZoom ideally should not be called on finish() (which is when the activity is destroyed). A better zoom preference saving approach would be to extend the WebView's WebViewClient, and override onScaleChanged(). So continuing Jorge's onCreate method:

    public void onCreate(Bundle savedInstanceState)
    {
    
        ....
    
        class MyWebViewClient extends WebViewClient
        {
            @Override
            public void onScaleChanged(WebView wv, float oldScale, float newScale)
            {
                SaveZoom();
            }
        }
    
        mWebView.setWebViewClient(new MyWebViewClient());
    }
    
    0 讨论(0)
  • 2020-12-10 09:34

    Perhaps you could try:

    http://d.android.com/reference/android/webkit/WebSettings.html#setDefaultZoom(android.webkit.WebSettings.ZoomDensity)

    &

    http://d.android.com/reference/android/webkit/WebSettings.html#getDefaultZoom()

    I haven't tried it myself but that looks like it might work

    0 讨论(0)
  • 2020-12-10 09:35

    its me Jorge from Grupo Reforma, this is my implementation using SharedPreferences..

    static final  String PREFS_Zoom = "PREFS_Zoom";
    
     private String zoomlevel;
     private int Default_zoomlevel=100;
    
         @Override
         public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
    
          mWebView = (WebView) findViewById(R.id.webview);        
    
          GetZoom();
          mWebView.setInitialScale(Default_zoomlevel);
    
          FrameLayout mContentView = (FrameLayout) getWindow().getDecorView().findViewById(android.R.id.content);
          View zoom = mWebView.getZoomControls();
          mContentView.addView(zoom, ZOOM_PARAMS);
          zoom.setVisibility(View.VISIBLE);
    
          WebSettings webSettings = mWebView.getSettings();
          webSettings.setSavePassword(false);
          webSettings.setSaveFormData(false);
          webSettings.setJavaScriptEnabled(true);
          webSettings.setSupportZoom(true);
    
          mWebView.loadUrl("http://www.elnorte.com");
        }
    
    
    
         private void GetZoom(){
          try{
           SharedPreferences settings = getSharedPreferences(PREFS_Zoom,0);
              zoomlevel = settings.getString("zoom_level","");     
           if (zoomlevel.length() >0)   
            Default_zoomlevel = Integer.parseInt(zoomlevel);               
           else
           Default_zoomlevel =100;
          }catch(Exception ex){
           Log.e("******ZOOM ! ", "Exception GetZoom()  ::"+ex.getMessage());          
          }
         }
    
    
         private void SaveZoom(){
          try{
           SharedPreferences settings = getSharedPreferences(PREFS_Zoom,0);            
           SharedPreferences.Editor editor = settings.edit();    
           Default_zoomlevel = (int) (mWebView.getScale() *100);
           editor.putString("zoom_level",""+ Default_zoomlevel);   
           editor.commit();
          }catch(Exception ex){
           Log.e("******ZOOM ! ", "Exception SaveZoom()  ::"+ex.getMessage());    
          }
         }
    
    
         public void finish() {  
          SaveZoom();
              super.finish();     
         }
    

    I hope this help

    0 讨论(0)
提交回复
热议问题