I am just starting learning android and i started making an experimental app.My goal is when i press the button ,the text should change in this way:when it is "OFF" ,pressing the button should make it "ON" and the opposite. Here is my code:
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.TextView);
Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
String text="";
if (textView.getText().equals("ON")) {
text="OFF";
} else if (textView.getText().equals("OFF")) {
text="ON";
}
textView.setText(text);
return false;
}
});
}
}
When i run it and press the button ,the app shows the "OFF" for a fraction of a second and then "ON" again.This happens every time i press the button."ON" is the default value.I just would like to know what am i missing here?