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 ?
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 ?
TextView text = (TextView) findViewById(R.id.yourTextViewId);
text.onClickListner(this);
@Override
public void onClick() {
String textOnTextView = text.getText().toString();
}
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();
}
});
If you want to split lines and display them in different color then refer to following links.
A button has onClick , I dont think that a TextView has onClick so that a user clicks it.
Correct me if i am wrong
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;
}
});
}