Reputation:
I am newbie to Android Development, I am looking to get an one more menu on ActionMode, that is along with cut,copy,selectAll. I want to add "mark" to this menu.
So I added an item in main.xml below is the following code of it.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.fn.MainActivity" >
<item
android:id="@+id/action_settings1"
android:orderInCategory="100"
android:onClick="onContextualMenuItemClicked"
android:title="Mark"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
This was tested and works fine in most of the phones, but in few phones it long pressing on texts, it shows the message
unfortunately, app has stopped
there is no other process and my app was closed suddenly.
I am not sure why? can someone assist me here?
If I remove this following line from that item, it will works.
android:onClick="onContextualMenuItemClicked"
but I am triggering that onclick in Activity,
public void onContextualMenuItemClicked(MenuItem item) {
/*switch (item.getItemId()) {
case R.id.mark:
webView.loadUrl("javascript:doMouseUp();");
break;
default:
System.out.println("default");
break;
}
if (mActionMode != null) {
mActionMode.finish();
}*/
}
@Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.clear();
mode.getMenuInflater().inflate(R.menu.main, menu);
}
super.onActionModeStarted(mode);
}
Upvotes: 1
Views: 361
Reputation: 1078
It looks like you are extending the MainActivity with ActionBarActivity
. This is supports only after the Android KitKat. In version 5 and later it doesn't make problem.
You need to extend the MainActivity with Activity, then it will works for you.
public class MainActivity extends Activity {
Then, You need to have separate xml file (myMenu.xml) under /res/menu in your project structure.
myMenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.fn.MainActivity" >
<item
android:id="@+id/mark"
android:showAsAction="always"
android:onClick="onContextualMenuItemClicked"
android:title="Mark">
</item>
</menu>
Copy this method and change as per your need and replace in Activity.
@Override
public void onActionModeStarted(ActionMode mode) {
if (mActionMode == null) {
mActionMode = mode;
Menu menu = mode.getMenu();
menu.clear();
mode.getMenuInflater().inflate(myMenu, menu);
}
super.onActionModeStarted(mode);
}
That's all, hope now it works for you.
Upvotes: 1
Reputation: 923
You can try something dynamic!! Override this method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
Make this Method
private void CreateMenu(Menu menu) {
MenuItem mnu1 = menu.add(0, 0, 0, "Delete");
{
//mnu1.setIcon(R..ic_menu_delete);
//R.drawable.ic_menu_delete
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
MenuItem mnu2 = menu.add(0, 1, 1, "Edit");
{
//mnu2.setIcon();
mnu2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
}
And for onClick override this
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuChoice(item);
}
and create this method
private boolean MenuChoice(MenuItem item) {
switch (item.getItemId()) {
case 0:
//your method here
return true;
case 1:
//your method here
return true;
}
return false;
}
My code creates 2 options
Upvotes: 0