1

In my web view i need to insert zoom, but without +/- navigation controls. So my objective is to hide this navigation controls(+/- buttons)

My code is:

package pack.ivoclar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class AppActivity extends Activity {

    String url = null;


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle); 

        Intent intent;
        intent = getIntent();
        url = intent.getStringExtra("url");

        loadPage();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch(item.getItemId())
        {
            case R.id.exit:
            {
                finish();
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);

        }


    }

    private void loadPage()
    {

        setContentView(R.layout.main);

        WebView myWebView;
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebChromeClient(new WebChromeClient());
        myWebView.getSettings().setBuiltInZoomControls(false);
        myWebView.getSettings().setSupportZoom(false);

        myWebView.loadUrl(url);


        myWebView.setWebViewClient
        (new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                view.loadUrl(url);

                return false;

            }

            @Override
            public void onReceivedError (WebView view, int errorCode, String description, String failingUrl)
            {

                Intent Error;
                Error = new Intent(getBaseContext(), ErrorActivity.class);
                Error.putExtra("url", failingUrl);
                startActivity(Error);
                finish();

            }

        });


    }

}

My android api level is 10 to work with android +2.3.3. Can u help me?

Simão Lemos
  • 1,170
  • 2
  • 8
  • 14

2 Answers2

0

You should use setDisplayZoomControls to allow the user to still use the pinch to zoom but the controls will not be displayed.

    myWebView.getSettings().setDisplayZoomControls(false);

To apply it in API 10,

you can refer this

Community
  • 1
  • 1
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

you need to create own custom WebView and do one hack for pre-honeycomb, like below:

public class WebViewNoZoomControllers extends WebView {

private ZoomButtonsController zoomControl = null;

public WebViewNoZoomControllers(Context context) {
    super(context);
    prepare();
}

public WebViewNoZoomControllers(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    prepare();
}

public WebViewNoZoomControllers(Context context, AttributeSet attrs) {
    super(context, attrs);
    prepare();
}

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void prepare(){
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        this.getSettings().setBuiltInZoomControls(true);
        this.getSettings().setDisplayZoomControls(false); //API11 and above only
    } else
        assignControllers();
}

private void assignControllers() {
    try {
        Class webview = Class.forName("android.webkit.WebView");
        Method method = webview.getMethod("getZoomButtonsController");
        zoomControl = (ZoomButtonsController) method.invoke(this, null);
    } catch (Exception e) {}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (zoomControl != null && zoomControl.isVisible())
        zoomControl.setVisible(false);
    return super.onTouchEvent(event);
}

}
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • dont work, generates an error in setDisplayZoomControls in prepare function ty – Simão Lemos Dec 10 '14 at 13:01
  • what error? don't say lint's errors... have you added `@SuppressWarnings("deprecation")` and `@SuppressLint("NewApi")`? (I've added them to source above) – snachmsm Dec 10 '14 at 13:26
  • error in setDisplayZoomControls and in HONEYCOMB. ty – Simão Lemos Dec 10 '14 at 14:51
  • again: what error? `setDisplayZoomControls` is native method, [see docs](http://developer.android.com/reference/android/webkit/WebSettings.html#setDisplayZoomControls(boolean)) and it have to run on Hoenycomb or later os version. – snachmsm Dec 10 '14 at 15:14
  • in setDisplayZoomControls is one api compatiblity error and in honeycomb is "cannot find" – Simão Lemos Dec 10 '14 at 15:26
  • your IDE probably don't see this level of api, so you probably have set compileWithSDK (not min and target values in manifest) for 10 or lower. change for 11 or even better - for highest possible. for Eclipse: right click on Project -> Properties -> Android -> check on list proper (or edit `project.properties` or `default.properties` placed in root folder of project, next to manifest) – snachmsm Dec 10 '14 at 15:35