0

I am having an issue with android webview default text selection. what i want to do is add an item to the default menu which appears on text selection in webview

I want to Add a Button to left of select all

The functionality i want to acieve is add a button to left side of select all. how can this be done

Umer Kiani
  • 3,783
  • 5
  • 36
  • 63

1 Answers1

3

you need to override the WebView class for this you need to use this code i am giving you demo code for changing defaut CAB after selection you can use your own contextual menu

public class MyWebView  extends WebView{
CustomizedSelectActionModeCallback actionModeCallback;
public Context context;
public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    this.context=context;
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
    // TODO Auto-generated method stub
    //      ViewParent parent = getParent();
   //        if (parent == null) {
     //            return null;
     //        }
    actionModeCallback = new CustomizedSelectActionModeCallback();
    return startActionModeForChild(this,actionModeCallback);

}

public class CustomizedSelectActionModeCallback implements ActionMode.Callback{

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
        return true;

    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        mode.setTitle("CheckBox is Checked");

        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.item_delete:
            clearFocus();
            Toast.makeText(getContext(), "This is my test click", Toast.LENGTH_LONG).show();
            break;

        default:
            break;
        }
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // TODO Auto-generated method stub


        clearFocus(); // this  is not clearing the text in my device having version 4.1.2
        actionModeCallback=null;

    }

}

}

MainActivity.java is

public class MainActivity extends Activity {

MyWebView web_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    web_view= (MyWebView)findViewById(R.id.webView1);
    web_view.loadUrl("file:///android_asset/menu_pages/Chemchapter1/OEBPS/Text/07_Chapter01.html");// you can load your html here


}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >


<com.rstm.webviewcheck.MyWebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>  

contextual_menu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item

android:id="@+id/item_delete"

android:icon="@android:drawable/ic_menu_delete"

android:showAsAction="ifRoom|withText"

android:title="Delete"

android:titleCondensed="Delete">

image description

Irshad Khan
  • 794
  • 6
  • 21
  • 1
    Thanks Bro it helped alot and working Perfectly :) JazakAllah – Umer Kiani May 19 '14 at 12:49
  • 1
    Bro can we add the native search button to this bar ? like it works in default i want the search functionality to work. or should i add custom code for it – Umer Kiani May 19 '14 at 13:15
  • 1
    Yeah! bro you need to write custom code for it add a search button in custom CAB and apply custom code. – Irshad Khan May 22 '14 at 05:24
  • 1
    It is my assumption that `onActionItemClicked` should return true, and `mode.finish()` must be called to close the CAB. You are not doing either of these things. How are you closing the CAB once an action is taken? – Sean Beach Jul 02 '14 at 14:38
  • 1
    This gets rid of the original menu items. What if I want to keep those items? – b.lyte Sep 12 '16 at 18:03