Boolean isStart = true; initialize a global variable
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stop = (Button) findViewById(R.id.start);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.start){
if(isStart){
stop.setText("Stop")
}else{
stop.setText("Start");
}
isStart = !isStart;
}
}
v.getId()==R.id.start
– In this code I am checking if you have clicked that button or not. If you have only 1 view which uses this
as onClickListener
, then there is no need for that check. If you have multiple items, you should check for which one is clicked (preferably with switch-case
).
CLICK HERE to find out more related problems solutions.