0

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)
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142

1 Answers1

0

Hope this helps

Check this answer https://stackoverflow.com/a/13004720/2826147, it has very good explaination with example as well as readout comment too.

TextView has a method setCustomSelectionActionModeCallback() which should be used instead of startActionMode(). Using this enables customisation of the menu used by TextView for text selection.

Sample code:

bodyView.setCustomSelectionActionModeCallback(new StyleCallback());

If you dont have space than you can remove any pre-populated menu items then add your own.

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        Log.e("", String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
        CharacterStyle cs;
        TextView bodyView = null;
        int start = bodyView.getSelectionStart();
        int end = bodyView.getSelectionEnd();
        SpannableStringBuilder ssb = new SpannableStringBuilder(bodyView.getText());

        /*Toast.makeText(getActivity(), (String)data.result,
                Toast.LENGTH_LONG).show();*/

        Toast.makeText(this, " toast shown ", Toast.LENGTH_LONG).show();


        switch (item.getItemId()) {

            case R.id.bold:
                cs = new StyleSpan(Typeface.BOLD);
                ssb.setSpan(cs, start, end, 1);
                bodyView.setText(ssb);
                return true;

            case R.id.italic:
                cs = new StyleSpan(Typeface.ITALIC);
                ssb.setSpan(cs, start, end, 1);
                bodyView.setText(ssb);
                return true;

            case R.id.underline:
                cs = new UnderlineSpan();
                ssb.setSpan(cs, start, end, 1);
                bodyView.setText(ssb);
                return true;
        }
        return false;
    }
Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142