Search code examples
androidcharsequence

How can I use " getCharSequence " in this code?


I have this code for sending a text is selected from first textView to second textView with clicking on a button :

txt=(TextView)findViewById(R.id.txt);
        txt2=(TextView)findViewById(R.id.txt2);
        btn=(Button)findViewById(R.id.btn);
        final int startIndex = txt.getSelectionStart();
        final int endIndex = txt.getSelectionEnd();
        final String stringYouExtracted = txt.getText().toString().substring(startIndex, endIndex);
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        clipboard.setText(stringYouExtracted);

        btn.setOnClickListener(new OnClickListener() {


            public void onClick(View arg0) {
                 txt2.setText(stringYouExtracted);


            }
        });

but when I press button nothing send to txt2 and txt2 is empty .... why ??

I think txt2.setText(stringYouExtracted) is wrong but I am very biginner and I can't fix it ????

Is public CharSequence getCharSequence (String key) a solution ?? if yes help me because I don't know how can I use it ????


Solution

  • Try this...

    Get the all text value inside ClickListener when application launch all the text are empty

        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                 final int startIndex = txt.getSelectionStart();
                 final int endIndex = txt.getSelectionEnd();
                 final String stringYouExtracted = txt.getText().toString().substring(startIndex, endIndex);
                 ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                 clipboard.setText(stringYouExtracted);
                 txt2.setText(stringYouExtracted);
            }
        });