3

I want to grab Text when user click on the TextView

For Example :

TextView string = "this is a test for android and textView"

When user click on textview in android position grab android
Anyone have a solution for this ?

Renjith
  • 5,783
  • 9
  • 31
  • 42
Araz Jafaripur
  • 927
  • 2
  • 12
  • 32

6 Answers6

1
TextView text = (TextView) findViewById(R.id.yourTextViewId);
text.onClickListner(this);


@Override
public void onClick() {
    String textOnTextView = text.getText().toString();
}
Pramod Ravikant
  • 1,039
  • 2
  • 11
  • 28
1

You can assign an onClick listener to the textview, make it final and then get its text.

    final TextView txt = (TextView) findViewById(R.id.txt);

    txt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            String getTxt = txt.getText().toString();
        }
    });
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
0

If you want to split lines and display them in different color then refer to following links.

Split text from string

Apply color to specific text

Community
  • 1
  • 1
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
0

A button has onClick , I dont think that a TextView has onClick so that a user clicks it.

Correct me if i am wrong

abhinav
  • 527
  • 3
  • 11
  • 24
0

If you want to select part of text, try to use EditText

Isaiah
  • 432
  • 2
  • 6
0

This is not a solution for your need. But only one step to solution.

Use setTextIsSelectable(boolean) or the TextView_textIsSelectable XML attribute to make the TextView selectable (text is not selectable by default).

Using following code, I managed to get selected text as String. You need to first select string by dragging over it.
NB: you need minimum API 11 to use setTextIsSelectable(boolean)

TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1=(TextView) findViewById(R.id.textView1);
t1.setTextIsSelectable(true);// IMPORTANT
        t1.setText("This is Android program");
        t1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()){
                case MotionEvent.ACTION_UP:
                    int start=t1.getSelectionStart();
                    int end=t1.getSelectionEnd();
                    String sub=t1.getText().subSequence(start, end).toString();
                    Toast.makeText(getBaseContext(), sub, 1).show();
                }
                return true;
            }
        });
}
Nizam
  • 5,698
  • 9
  • 45
  • 57