Tuesday, January 26, 2016

android - How to disable WebView text selection and context menu

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"
    >
    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</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.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private WebView mWebView;

    private String mUrl="http://developer.android.com/guide/index.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Request window feature action bar
        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 bar color
        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#FF316370"))
        );

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mWebView = (WebView) findViewById(R.id.web_view);

        // Request to render the web page
        renderWebPage(mUrl);

    }

    // 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
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // Do something when page loading finished
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int newProgress) {
            }
        });

        // Enable the javascript
        mWebView.getSettings().setJavaScriptEnabled(true);

        /*--- single and perfect setting to disable text selection and context menu in web view --*/
        /*
            public void setOnLongClickListener (View.OnLongClickListener l)
                Register a callback to be invoked when this view is clicked and held.
                If this view is not long clickable, it becomes long clickable.

            Parameters
                l : The callback that will run
        */
        // Set a long click listener for web view
        mWebView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // For final release of your app, comment the toast notification
                Toast.makeText(mContext,"Long Click Disabled",Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        /*------ additional settings to disable web view context menu and text selection ---------/*
        /*
            public void setLongClickable (boolean longClickable)
                Enables or disables long click events for this view. When a view is long clickable
                it reacts to the user holding down the button for a longer duration than a tap.
                This event can either launch the listener or a context menu.

            Related XML Attributes
                android:longClickable
            Parameters
                longClickable : true to make the view long clickable, false otherwise
        */
        // Disable the long click for web view
        mWebView.setLongClickable(false);

        /*
            public void setHapticFeedbackEnabled (boolean hapticFeedbackEnabled)
                Set whether this view should have haptic feedback for events such as long presses.

                You may wish to disable haptic feedback if your view already controls its
                own haptic feedback.

            Related XML Attributes
                android:hapticFeedbackEnabled
            Parameters
                hapticFeedbackEnabled : whether haptic feedback enabled for this view.

        */
        // Disable haptic feedback for web view
        mWebView.setHapticFeedbackEnabled(false);

        // Render the web page
        mWebView.loadUrl(urlToRender);
    }
}