Example usage for android.webkit WebView.FindListener WebView.FindListener

List of usage examples for android.webkit WebView.FindListener WebView.FindListener

Introduction

In this page you can find the example usage for android.webkit WebView.FindListener WebView.FindListener.

Prototype

WebView.FindListener

Source Link

Usage

From source file:com.free.searcher.MainFragment.java

private void initFind() {
    container = (RelativeLayout) activity.findViewById(R.id.layoutId);
    findBox = (EditText) container.findViewById(R.id.findBox);
    findBox.setText(currentSearching);/*from   w  ww  .j av  a2 s. c o m*/
    findBox.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

    findBox.setOnKeyListener(new OnKeyListener() {
        @SuppressWarnings("deprecation")
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.d("onKey", "keyCode=" + keyCode + ",event=" + event + ",v=" + v);
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER))) {
                webView.findAllAsync(findBox.getText().toString());
            }
            return false;
        }
    });

    //      findBox.setOnFocusChangeListener(new OnFocusChangeListener() {
    //            @Override
    //            public void onFocusChange(View p1, boolean p2) {
    //               webView.findAllAsync(findBox.getText().toString());
    //            }
    //         });

    findRet = (TextView) container.findViewById(R.id.findRet);

    backButton = (Button) container.findViewById(R.id.backButton);
    backButton.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
    backButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (findBox.isFocused()) {
                webView.findAllAsync(findBox.getText().toString());
            }
            webView.requestFocus();
            webView.findNext(false);
        }
    });

    nextButton = (Button) container.findViewById(R.id.nextButton);
    nextButton.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
    nextButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (findBox.isFocused()) {
                webView.findAllAsync(findBox.getText().toString());
            }
            webView.requestFocus();
            webView.findNext(true);
        }
    });

    clearButton = (Button) container.findViewById(R.id.clearButton);
    clearButton.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
    clearButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            findBox.setText("");
            webView.findAllAsync(findBox.getText().toString());
            findBox.requestFocus();
        }
    });

    closeButton = (Button) container.findViewById(R.id.closeButton);
    closeButton.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
    closeButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showFind = false;
            container.setVisibility(View.INVISIBLE);
        }
    });

    webView.findAllAsync(findBox.getText().toString());
    webView.setFindListener(new WebView.FindListener() {
        @Override
        public void onFindResultReceived(int p1, int p2, boolean p3) {
            findRet.setText((p1 + (p2 > 0 ? 1 : 0)) + "/" + p2);
        }
    });
    if (showFind) {
        container.setVisibility(View.VISIBLE);
    } else {
        container.setVisibility(View.INVISIBLE);
    }
}