Reputation: 2609
Really stupid question here, but could not find an answer googling.
I have a clickable textview. How to retrieve the row's number that is clicked??
Upvotes: 0
Views: 803
Reputation: 86948
The question isn't that stupid because there is no built-in method, that I know of, to do this.
However you can use:
getLineHeight()
to determine how tall each line is in the TextView andMotionEvent#getY()
in an OnTouchListener to calculate which line was clicked.Basic example:
textView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
float line = FloatMath.floor(event.getY() / textView.getLineHeight());
Toast.makeText(getBaseContext(), "" + line, Toast.LENGTH_SHORT).show();
}
return false;
}
});
From the getLineHeight()
documentation:
Note that markup within the text can cause individual lines to be taller or shorter than this height, and the layout may contain additional first- or last-line padding.
So if you have modified any attribute listed above, you'll need to account for that as well.
Upvotes: 3