1

How to create custom menu for webview when longpress event occur as shown in image at the topFrom playbooks application?

 public class MainActivity extends Activity {

private String data;
private WebView webview;
private String clipdata = "";
private boolean mark_text;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = (WebView) this.findViewById(R.id.webView1);

    data = "The entire plan-to-produce process, including enterprise-level planning and scheduling, plant-level operations, manufacturing execution, batch manufacturing, and quality management. Capabilities for Big Data management and process integration support the use of real-time data from the shop floor to maintain batch traceability and genealogy. Embedded quality and compliance controls enable process manufacturers to manage exceptions and address nonconformance through corrective and preventive actions for batches. Leveraging mobile and cloud as well ![enter image description here][2]as on-premise technologies, this level of production control helps increase throughput, set predictable and shorter cycle times, improve asset utilization, and help ensure that inventory targets are met.</body></html>";
    webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_search:
        Toast.makeText(this, "action_search selected", Toast.LENGTH_SHORT)
                .show();
        return true;
    case R.id.action_settings:
        Toast.makeText(this, "action_settings selected", Toast.LENGTH_SHORT)
                .show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

}

enter image description here

I have update answer with my code. plz check it and say how how can I disable this default action bar and add my own action bar but at the same time text selection functionality should work.

fidato
  • 719
  • 5
  • 22
nil
  • 187
  • 1
  • 18

3 Answers3

1

This is called a Contextual Action Bar, it's an overlay on top of the default action bar. There is a good tutorial here which describes how to work with it.

To get own action bar to work there while still keeping the selection functionality is going to be tricky to say the least I'm afraid...

Toon Borgers
  • 3,638
  • 1
  • 14
  • 22
  • okay. but can I provide my own selection functionality in webview? – nil Dec 24 '13 at 11:25
  • Apparantly, there's an [open issue](https://code.google.com/p/android/issues/detail?id=24841) for this with the android team. So unfortunately, you can't do this as of yet. – Toon Borgers Dec 24 '13 at 11:50
0

To add actions to the action bar, create a new XML file in your project's res/menu/ directory.

main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="never" />
</menu>

Add the Actions to the Action Bar

To place the menu items into the action bar, implement the onCreateOptionsMenu() callback method in your activity to inflate the menu resource into the given Menu object. For example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}

Respond to Action Buttons

When the user presses one of the action buttons or another item in the action overflow, the system calls your activity's onOptionsItemSelected() callback method. In your implementation of this method, call getItemId() on the given MenuItem to determine which item was pressed—the returned ID matches the value you declared in the corresponding element's android:id attribute.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
             Toast.makeText(this,"action_search selected",Toast.LENGTH_SHORT).show();
            return true;
        case R.id.action_settings:
           Toast.makeText(this,"action_settings selected",Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.on

OptionsItemSelected(item);
    }
}

for further guidance refer the official doc

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
0

first create your own ActionModeCallBack then you need to create a class and extend WebView and overwrite this method:

public ActionMode startActionMode(ActionMode.Callback callback) 
{
    actionModeCallback = new CustomizedSelectActionModeCallback();
    return super.startActionMode(actionModeCallback);
}    

update: look at this
It's work for me...

Community
  • 1
  • 1
MHP
  • 2,613
  • 1
  • 16
  • 26