Selecting text in textview - android

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);

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 add spinner to contextual action bar?

like my tittle, i want to ask...
how to add spinner on contextual action bar like in galery android?
now i just can set the tittle like "1 selected" "2 selected"
here`s my code
public void lvMainOnLongItemClick(){
lvMain.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lvMain.setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#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
adapter.clearSelection();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
nr = 0;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.canvas_main, menu);
menuEdit = menu.findItem(R.id.menu_edit);
menuUpload = menu.findItem(R.id.menu_upload);
menuUpload.setVisible(false);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_edit:
Toast.makeText(rbkMain.this, "Edit", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_cancel:
Toast.makeText(rbkMain.this, "Cancel", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_copy:
Toast.makeText(rbkMain.this, "Copy", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_upload:
Toast.makeText(rbkMain.this, "Upload", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(rbkMain.this, "Yihaa", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position,long id, boolean checked) {
// TODO Auto-generated method stub
if (checked) {
nr++;
adapter.setNewSelection(position, checked);
} else {
nr--;
adapter.removeSelection(position);
}
if(nr > 1)
menuEdit.setVisible(false);
else
menuEdit.setVisible(true);
mode.setTitle(nr + " selected");
}
});
lvMain.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
lvMain.setItemChecked(position, adapter.isPositionChecked(position));
return false;
}
});
thx a lot for your help... happy coding
For regular ActionBar this worked for me Adding spinner to ActionBar (not Navigation but for the contextual action bar it was not working. This is how I made it work, see if it can help you.
For your listview listener something like this:
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(YOUR_MENU_XML, menu);
filterSpinner = (Spinner) MenuItemCompat.getActionView(menu.findItem(R.id.menu_overlay_spinner));
filterSpinner.setAdapter(send2Adapter);
return true;
}
The menu xml should have something like this:
<item
android:icon="#drawable/filter"
android:id="#+id/menu_overlay_spinner"
android:title="Spinner"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="ifRoom"
android:actionLayout="#layout/YOUR_ACTION_LAYOUT"/>
Finally the actionLayout is just a layout with a Spinner as the root:
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In onCreate of main activity
// This has to be called before setContentView and you must use the
// class in android.support.v4.view and NOT android.view
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
To show/hide progress in action bar. Notice with actionbarsherlock you must use boolean.TRUE/FALSE, not just true/false.........
if (getSupportLoaderManager().hasRunningLoaders()) {
setProgressBarIndeterminateVisibility(Boolean.TRUE);
} else {
setProgressBarIndeterminateVisibility(Boolean.FALSE);
}

Contextual Action Bar in Webview text Selection

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

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();
}

How to set the background of checked items in a listview in multiple select mode in android

I've a ListActivity
public class MyActivity extends ListActivity{
protected void onCreate(Bundle savedstate){
super.onCreate(savedstate);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Log.i("xxx", "onPrepareActionMode");
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;
}
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
Log.i("xxx", "pos: "+position+", checked: "+checked);
mode.setTitle(getListView().getCheckedItemCount()+" checked");
}
});
}
}
Here I can see the items getting checked but background of checked items is not changing. I know a way how to do it, have your CustomAdapter and in getView() set the background of the views that are checked. But I believe there is much simpler way to handle it. Could you tell me if there is anyway to achieve it?
Attaching the Screenshot
Ok, i got the answer by going through the source code of other app which does that. Set a statelist drawable as background for list item. In selector state_activated = true give the background drawable.

Categories

Resources