2014年5月8日 星期四

Customise WebView Contextual Menu on Android

Useful link:
http://techiepulkit.blogspot.hk/2014/02/android-overriding-default-contextual.html

Steps:
1. Define custom context menu using xml and put them in res/menu folder of the Module project
{module_project_dir}/platform/android/res/menu/context_menu.xml

<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_highlight"
android:title="@string/highlight"
android:showAsAction="always"/>
<item android:id="@+id/action_addNote"
android:title="@string/addNote"
android:showAsAction="always"/>
</menu>

For xml syntax, refer to http://developer.android.com/guide/topics/resources/available-resources.html

2. Define the string value in strings.xml if neccesary
and put them in res/values folder
{module_project_dir}/platform/android/res/values/strings.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="highlight">Highlight</string>
<string name="addNote">Add Note</string>
</resources> 

For xml syntax, refer to http://developer.android.com/guide/topics/resources/available-resources.html

Note: If xml in step 1 and 2 are invalid, will raise the Error: Unable to generate R.java 
Say, if context_menu.xml is referring string value using id "@string/highlight" but it is not defined in strings.xml, it will raise the error.

3. In Module's java source, create class to extends WebView. Override startActionMode method and implement ActionMode.Callback.
onCreateActionMode, inflat context menu using xml defined in Step 1.

e.g.
actionmode.getMenuInflater().inflate( TiRHelper.getApplicationResource("menu.context_menu"), menu);

P.S. to refer to strings value defined in Step 2, use TiRHelper.getApplicationResource("id.xxxx");





沒有留言:

張貼留言