Contextual Action Bar in Webview text Selection - android

I found a lot of question about this problem, but I can't fix it. I am creating custom contextual action bars (CAB) within WebView i followed this Tutorial
My problem is CAB working fine in setOnLongClickListener but i cant able to select the text.
I referred these links:
Link 1, Link 2
Edit :
mywebview.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mActionMode = MainActivity.this.startActionMode(new ActionBarCallBack());
return true;
}
});
Implement the ActionMode.Callback interface:
class ActionBarCallBack implements ActionMode.Callback {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
#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 void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
}
I Tried like this :
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
mActionMode = Contents.this.startActionMode(new ActionBarCallBack());
}
Help me where i am wrong. Thank in advanced

Related

Finishing Contextual Action Bar on CHOICE_MODE_MULTIPLE_MODAL

I have a ListView and when the user long-presses on any particular item, the CAB starts.
I am using,
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
and the MultiChoiceModeListener to intercept the callbacks,
getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
}
});
Now, in my onResume() (outside this listener), I want to check if my ActionMode is visible/invisible and if it is, then depending upon certain conditions, I want to manually disable/enable it.
How can I do this?
I waited for a day but didn't get any answer on this and solved it myself. I don't know if the approach is perfect.
Declare a field ActionMode
ActionMode mActionMode;
Now in the onCreateActionMode() method you get the ActionMode,
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mActionMode = mode;
}
And in the onDestroyActionMode(),
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
Now anywhere in your code just check if the mActionMode is null or not to check if the ActionMode is enabled or disabled.
Hope this helps. Do post an answer if you have a better solution.

How to hide the menu when using multiple select in GridView in Android

I use GridView and implement multiple select.
When I select the multiple item from GridView , it will show the menu on the top of screen.
But I don't want the menu show on the top .
I set the GridView to CHOICE_MODE_MULTIPLE_MODAL and implement thr MultiChoiceModeListener of class.
Does there has any method can hide the menu when select the multiple item ?
The code of class is like the following
public class LocalFileBrowserFragment extends Fragment implements MultiChoiceModeListener
The code of setup the GridView is like the following:
private GridView fileListView;
fileListView = (GridView) view.findViewById(R.id.browserList) ;
fileListView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
fileListView.setMultiChoiceModeListener((MultiChoiceModeListener)this);
fileListView.setNumColumns(4);
And the code of MultiChoiceModeListener in GridView is like the following:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
Log.i(TAG, "onCreateActionMode");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
for (int i = 0; i < fileListView.getCount(); i++) {
fileListView.setItemChecked(i, false);
mSelectMap.clear();
}
mFileListAdapter.notifyDataSetChanged();
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
//Log.i(TAG, "onPrepareActionMode");
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
mSelectMap.put(position, checked);
mFileListAdapter.notifyDataSetChanged();
}
I set the GridView to CHOICE_MODE_MULTIPLE_MODAL and implement thr MultiChoiceModeListener of class.
I don't want the menu show on the top.
Does there has any method can hide the menu when select the multiple item ?
I solved the problem, I used the following to show the bar.
bar = getActivity().getActionBar();
bar.show();
Save ActionMode as a field of your MultiChoiceModeListener in onCreateActionMode
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
theActionMode = mode;
return true;
}
Then finish the ActionMode in onItemCheckedStateChanged
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
// Your CheckedChange Code......
theActionMode.finish();
}

SearchView onMenuItemActionCollapse Not Working

I'm using SearchView while my activity is extanding Sherlock Library.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.countriesxml, menu);
SearchView mSearchView = (SearchView) menu.findItem(R.id.searchall)
.getActionView();
mSearchView.setQueryHint("Search for smartphones");
MenuItem menuItem = menu.findItem(R.id.searchall);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
item.collapseActionView();
indicator.setVisibility(View.VISIBLE);
pager.setVisibility(View.VISIBLE);
lvth.setVisibility(View.GONE);
return true;
}
});
return;
}
Please note that i tried to use OnCloseListener() but it didn't work.
What's wrong?
have a look at your code at this section:
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
Replace it with below code and try again.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
Try using OnFocusChangeListener instead
menuItem.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean queryTextFocused) {
if (!queryTextFocused) {
// Code here
}
}
});

EditText setOnCreateContextMenuListener handle result

I have set setOnCreateContextMenuListener to edittext. onCreateContextMenu method is called user long press on edittext. and it opens context menu with 'done' and 'copy' options.
But my question is how can I handle when user select done option or copy options?
Can I get any event when user click on done button or copy button. so I can get selected text via clip manager?
edit.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
Log.i("TAG", "onCreateContextMenu");//it is printing while context menu is created.
}
});
Thanks.
onContextItemSelected is the event you should use. but it has no View argument to access selected item. Here is a trick to access to selected View.
public class MainActivity extends Activity
{
protected View selectedView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
this.selectedView = v;
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getId())
{
case R.id.Context_Edit:
// access to view with this.selectedView
break;
case R.id.Context_Delete:
// access to view with this.selectedView
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
You can get your both button event like following example
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.lv1)
{
menu.setHeaderTitle("Select Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 0, "Delete");
}
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getTitle() == "Edit")
{
// do something
}
if(item.getTitle() == "Delete")
{
// do something
}
return true;
}
Hope it will help you.
You must to override the onLongClick of the editText and setting one CAB. This is example:
private ActionMode.Callback action = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == android.R.id.copy){
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Log.i("CAB",String.valueOf(clipboard.getPrimaryClip().getItemAt(0).getText()));
}
return true;
}
};
private EditText edtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtText = (EditText) findViewById(R.id.edtText1);
registerForContextMenu(edtText);
edtText.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
((EditText)v).setTextIsSelectable(true);
((EditText)v).setCustomSelectionActionModeCallback(action);
return false;
}
});
}

Selecting text in textview

I have two textview and I want to implement the textselection feature in both the textview. The Api version is 15 and above. the xml for both the textview is same.
In my code, I have called the method,
tv1.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});
tv2.setCustomSelectionActionModeCallback(new Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});
When I run the code, I am successfully able to select the first textview but not able to select the second textview. Getting the logcat message as Textview is not selectable, action mode is cancelled. I have gone through all the links but not able to resolved the issue.
The xml structure for both the textview is same.
I have also added the:
tv1.setTextIsSelectable(true);
tv2.setTextIsSelectable(true);

Categories

Resources