Wednesday, 17 April 2013

Select + copy text in a TextView?

 Generally we can copy / paste the value from EditText by long click. It is in-build functionality of Android. Now lets say you have requirement same thing in TextView. Then you have to use registerForContextMenu.


 Like  registerForContextMenu(yourtextview);

 and your TextView will be registered for receiving context menu events.

Then You have to override onCreateContextMenu() method. Following is entire code.


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtcopytext = (TextView) findViewById(R.id.txtcopytext);
        registerForContextMenu(txtcopytext);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // user has long pressed your TextView
        menu.add(0, v.getId(), 0,
                "Copy");

        // cast the received View to TextView so that you can get its text
        TextView yourTextView = (TextView) v;

        // place your TextView's text in clipboard
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        clipboard.setText(yourTextView.getText());
    }






 


8 comments:

  1. this good for the entire texts, what about just part of texts or specific text? how to make it?

    ReplyDelete
  2. you are posting a good information for people and keep maintain and give more update too.
    seo company in india

    ReplyDelete
  3. Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much

    Personal Installment Loans
    Title Car loan
    Cash Advance Loan

    ReplyDelete
  4. The content is very accurate and easily explaineed it is simulating as to waiting for your post again soon.

    Selenium Training in Chennai

    ReplyDelete
  5. This blog is having the general information.Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog.This is very interesting and useful.

    Hadoop Training in Chennai

    ReplyDelete
  6. Content is very clear and easily explained it is very interesting as well. I hope you post again soon.

    BIGDATA Training in Chennai

    ReplyDelete
  7. All the latest updates from the Python Automationminds team. Python Automationminds lets you program in Python, in your browser. No need to install any software, just start coding straight away. There's a fully-functional web-based console and a programmer's text-editor
    Phyton training in Chennai

    ReplyDelete
  8. Testers can build, enhance, and maintain scripts to regression test their mobile applications. Hands-on instruction is provided for those who want to explore the power of using Appium. The course covers content from installation to execution and reporting . The focus is on the practical application of Appium to resolve common mobile automated testing challenges. This course focuses on getting started with Appium.
    course/appiumtraininginsholinganallur/

    ReplyDelete