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






 


4 comments:

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

    ReplyDelete
  2. 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
  3. Content is very clear and easily explained it is very interesting as well. I hope you post again soon.

    BIGDATA Training in Chennai

    ReplyDelete
  4. Namun secara praktek, susunan teratas dalam permainan ini sering kali menjadi penentu kemenangan bettor dalam bermain
    asikqq
    dewaqq
    sumoqq
    interqq
    pionpoker
    bandar ceme terpercaya
    hobiqq
    paito warna terlengkap
    bocoran sgp

    ReplyDelete