question

nacnicnuc avatar image
nacnicnuc asked

How to use contextual action mode on Kindle Fire HDX

In my app , TextView call setCustomSelectionActionModeCallback(ActionMode.Callback) and show ActionMode menu.When TextView was long-clicked, It worked well on Android devices. But it does not work on Kindle Fire HDX, showing popup window(e.g. select all,copy). Are there any ways to use contextual action mode on Kindle Fire HDX?
fire tablet
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Nacnicnuc, Thank you for bringing this up. I tested following code in Kindle HDX and 2 Android devcies (4.1.2 and 4.4) private void editTextTest() { EditText editText = (EditText)findViewById(R.id.editText); editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { System.out.println("AndroidNativeTestActivity.onCreateActionMode"); return false; } @Override public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { System.out.println("AndroidNativeTestActivity.onPrepareActionMode"); return false; } @Override public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { System.out.println("AndroidNativeTestActivity.onActionItemClicked"); return false; } @Override public void onDestroyActionMode(ActionMode actionMode) { System.out.println("AndroidNativeTestActivity.onDestroyActionMode"); } }) I saw that, on a long press on the editText element onCreateActionMode() is being called only on Android 4.4 device. The same method was never called back in Kindle HDX and Android 4.1.2. I think that's an issue in Android OS and that's fixed in most recent version (4.4) of Android OS. HDX runs on 4.2.2 (API level 17). If you do not agree, please provide the code you are using in your app. HDX devices will be updated with 4.4 eventually and I can not promise on the ETA for that at this point. And the issue would be fixed when we would have OTA update for HDX with 4.4.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

nacnicnuc avatar image
nacnicnuc answered
Thanks for your reply. The following is my test code.I tested on Nexus7(Android 4.2.2), Sony Experia (4.0.4), Kindle Fire HDX(system version is 13.3.2.1). It works fine on Nesus7 and Experia. -------------------------------------------------------------- @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); textView = (TextView)rootView.findViewById(R.id.textview); textView.setTextIsSelectable(true); /* * actionMode from setCustomSelectionActionModeCallback works fine on Nexus7(Android 4.2.2) * this callback does not work on Kindle fire? */ textView.setCustomSelectionActionModeCallback(mActionModeCallback); return rootView; } private ActionMode.Callback mActionModeCallback = new ActionMode.Callback(){ @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { int id = item.getItemId(); switch(id){ case R.id.menu1 : return true; case R.id.menu2 : return true; case R.id.menu3 : return true; } return false; } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); return true; } @Override public void onDestroyActionMode(ActionMode mode) { } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } }; -------------------------------------------------------------- In your test code, onCreateActionMode() return false. I think, onCreateActionMode() should return true . In addition, I found another different. on Android devices, text selection was canceled and actionmode finished with back key ( maybe focus change). on Kindle Fire , text selection was not canceled with back key. thanks.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Nacnicnuc, Thanks for the details. Strange thing. I ran the same code (returned true from onCreateActionMode()) in two non Amazon devices of same OS and got different results. 1. Sony Experia - LT26ii - Android OS 4.1.2 - It worked perfectly fine. While long pressed on the text written on a TextView, onCreateActionMode() and onPrepareActionMode() are called eventually. While back button is pressed after that onDestroyActionMode() is called back. 2. Samsung - GT I9300 - Android OS 4.1.2 - It did not work. Text selector appeared but none of the methods called back. Same thing happens for HDX as well. I found below code to work in HDX. Can you check whether this is meeting your need or not. Please get back. TextView textView = (TextView)findViewById(R.id.editText); textView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { AndroidNativeTestActivity.this.startActionMode(new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { System.out.println("AndroidNativeTestActivity.onCreateActionMode"); return true; } @Override public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { System.out.println("AndroidNativeTestActivity.onPrepareActionMode"); return false; } @Override public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { System.out.println("AndroidNativeTestActivity.onActionItemClicked"); return false; } @Override public void onDestroyActionMode(ActionMode actionMode) { System.out.println("AndroidNativeTestActivity.onDestroyActionMode"); } }); return false; } });
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

nacnicnuc avatar image
nacnicnuc answered
Hi, Sujoy. Thanks to you, I could found the solution. The following code worked as intended on Kindle Fire HDX. -------------------------------------------------- //setOnLongClickListener() and startActionMode() instead of setCustomSelectionActionModeCallback mTextView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { getActivity().startActionMode(new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { int id = item.getItemId(); switch(id){ case R.id.menu1 : // do something mode.finish(); return true; case R.id.menu2 : // do something mode.finish(); return true; case R.id.menu3 : // do something mode.finish(); return true; } return false; } @Override public void onDestroyActionMode(ActionMode actionMode) { //close text selector on textview mTextView.clearFocus(); } }); // this return value(never true) was the point! return false; } }); -------------------------------------------------- I really appreciate your help.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Keypal avatar image
Keypal answered
Hi, Sujoy, I use the Android method, setCustomSelectionActionModeCallback(), on my Nexus 7 and on my Galaxy S5, and I also set clickable actions that I want to be available on the resulting Contextual Action Bar (CAB), that overlays the regular Action Bar. Thus prepared, on my Kindle HDX, as on my Google mobiles, a longclick causes the longpressed text to be highlighted and selection handles to appear. However, the Contextual Action Bar does not appear; instead, on the Kindle HDX, a floating "select all/copy/share" menu appears just above the selected text. I really love the Amazon mobile and want my app to work on it like it does on Google's. Not being able to find any Kindle documentation on the available Amazon Android methods and their differences from the same Google methods (does any such Amazon documentation exist?), I tried NacNicNuc's excellent template below, and the CAB did appear! However, the floating "select all/copy/share" menu also appeared, at the same time the CAB appeared. (Both also have to be dismissed separately.) Can you tell me, is there a programmatic way to disable this little "select all/copy/share" menu so that it does not appear at all when text is selected in a Kindle HDX Textview, without affecting the CAB ? And what is this little menu called? Does it have an individual name, as the Contextual Action Bar does?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

swati-g avatar image
swati-g answered
You can remove default action item in onCreateActionMode method. public boolean onCreateActionMode(ActionMode mode, Menu menu) { Log.d(TAG, "onCreateActionMode"); MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.style, menu); // inflate custom action item menu.removeItem(android.R.id.selectAll); // removed default selectAll. similary you can remove cut, copy, paste. return true; }
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

DougM@Amazon avatar image
DougM@Amazon answered
Hello Swati_g, Thank you for the suggestion. To help future developers implement your suggestion, we would like to point out that you must implement the android.view.ActionMode.Callback interface. For more information about this interface, please refer to the Android documentation at: https://developer.android.com/reference/android/view/ActionMode.Callback.html#onCreateActionMode(android.view.ActionMode,%20android.view.Menu) . And the Android R.id fields are referenced at the following site: http://developer.android.com/reference/android/R.id.html
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.