This should work:
text.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
text.setTextColor(Color.GREEN);
return false;
}
});
Also this should be in the onCreate like this:
public class MainActivity extends AppCompatActivity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
text.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
text.setTextColor(Color.GREEN);
return false;
}
});
}
}
But I think u mixed up onTouchListener and onClickListener. For only clicks I would recommend using onClickListener. But u were almost there u mixed up both xD.
With onClickListener
public class MainActivity extends AppCompatActivity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text.setTextColor(Color.GREEN);
}
});
}
}
EDIT
To toggle the color you can use this:
private TextView text;
private boolean toggle = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeColor(toggle);
}
});
}
private void changeColor(boolean toggle){
if(toggle){
text.setTextColor(Color.BLACK);
}else{
text.setTextColor(Color.GREEN);
}
this.toggle = !toggle;
}