I have this code:
public class MainActivity extends AppCompatActivity {
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = findViewById(R.id.tv);
tv.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ResourceAsColor")
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Integer mOffset = tv.getOffsetForPosition(motionEvent.getX(), motionEvent.getY());
Toast.makeText(MainActivity.this, findWordForRightHanded(tv.getText().toString(), mOffset), Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
private String findWordForRightHanded(String str, int offset) {
if (str.length() == offset) {
offset--;
}
if (str.charAt(offset) == ' ') {
offset--;
}
int startIndex = offset;
int endIndex = offset;
try {
while (str.charAt(startIndex) != ' ' && str.charAt(startIndex) != '\n') {
startIndex--;
}
} catch (StringIndexOutOfBoundsException e) {
startIndex = 0;
}
try {
while (str.charAt(endIndex) != ' ' && str.charAt(endIndex) != '\n') {
endIndex++;
}
} catch (StringIndexOutOfBoundsException e) {
endIndex = str.length();
}
char last = str.charAt(endIndex - 1);
if (last == ',' || last == '.' ||
last == '!' || last == '?' ||
last == ':' || last == ';') {
endIndex--;
}
return str.substring(startIndex, endIndex);
}
}
From: How to display each clicked words from TextView
Now, I want to replace Toast message with command change color of that clicked word to blue, but I don't know how. Can you help me. Thank you for your help.