please help me how can I do this: I have textView in my project and I want do this:
- select text
- do some actions like copy,save,... on texts
I find out this code for selecting part:
text= (TextView) findViewById(R.id.speech);
registerForContextMenu(text);
text.setTextIsSelectable(true);
and the default Sellection texs
with copy and select all actions are shown.
now my question is that how can I customize it and add other actions to it?
this is my class:
public class SellectedTexts extends AppCompatActivity implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Log.d(Log, "onCreateActionMode");
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.text_select, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Log.d(Log, String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
switch(item.getItemId()) {
case R.id.save:
File outputTXT =new File(Environment.getExternalStorageDirectory(), "App Folder");
outputTXT.mkdir();
if (!outputTXT.exists() && !outputTXT.mkdirs()) { //Can't make path to save pic.
//Log.d(DEBUG_TAG, "Can't create directory to save image");
Toast.makeText(this, "File can't save.",Toast.LENGTH_LONG).show();
}
Calendar calender =Calendar.getInstance();
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyymmdd_hhmmss");
String fileName =outputTXT.getPath() + File.separator + simpleDateFormat.format(calender.getTime())+" .txt";
File pictureFile =new File(fileName);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(MainActivity.speech_text.getText().toString().getBytes());
fos.close();
Toast.makeText(this, "File save in ExternalCard/Type-Yar folder.",Toast.LENGTH_LONG).show();
} catch (Exception error) { //Can't save image.
// Log.d(DEBUG_TAG, "File not saved: " + error.getMessage());
Toast.makeText(this, "File can't save.", Toast.LENGTH_LONG).show();
}
mode.finish();// Finish and close the ActionMode
return true;
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
}
logcats:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: , PID: 5224
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:91)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at android.widget.Toast.<init>(Toast.java:132)
at android.widget.Toast.makeText(Toast.java:434)
at .SellectedTexts.onActionItemClicked(SellectedTexts.java:57)
at android.widget.Editor$SelectionActionModeCallback.onActionItemClicked(Editor.java:3373)
at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:3946)
at android.support.v7.view.SupportActionModeWrapper$CallbackWrapper.onActionItemClicked(SupportActionModeWrapper.java:168)
at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onActionItemClicked(AppCompatDelegateImplV7.java:1703)
at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onActionItemClicked(AppCompatDelegateImplV7.java:1703)
at android.support.v7.view.StandaloneActionMode.onMenuItemSelected(StandaloneActionMode.java:136)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)