-1

I am developing an Sudoku solver app in Android. I have done designing in Table Layout and each cell has one TextView.

How to select a particular TextView by clicking it and set its text through button click?

I want like, on Button click which ever TextView is selected should change it't Text.

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
saksham7
  • 19
  • 5

2 Answers2

0

Use below code to copy the text when you long press on your textview :

Reference : How do I enable standard copy paste for a TextView in Android?

textView.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            ClipboardManager cManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData cData = ClipData.newPlainText("text", getText());
            cManager.setPrimaryClip(cData);
            Util.toast(mContext, string.text_copyed);
            return true;
        }
    });
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
  • Actually I am not asking about copying text.I want to select a TextView by clicking it as I have 9*9 text views present and then write to it a value by clicking buttons from 1 to 9 – saksham7 Feb 12 '18 at 19:33
0

You can keep one variable with currentSelectedNumber. On each TextView.onTouchListener update this variable value

And While clicking button, based on value of this variable update that TextView.

Sample Code:

int currentSelectedNumber = 0;
tv1.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                    currentSelectedNumber = 1;                        
                    return true;
                    }
                });
tv2.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                    currentSelectedNumber = 2;                        
                    return true;
                    }
                });

On Button Click:

btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (currentSelectedNumber){
                            case 0:{
                                tv1.setText("Your Text");
                                break;
                            }
                            case 1:{
                                tv2.setText("Your Text");
                                break;
                            }
                        }
                    }
                });
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22