I have a lot of button inside my application and for every button i need to create onClickListener, this will incerase line of code’s.
So i create a class for onClickListener below:
import android.app.Activity;
import android.content.Intent;
import android.view.View;
public class OnClickListener implements android.view.View.OnClickListener{
Activity current;
Class<?> goTo;
public OnClickListener(Activity arg0, Class arg1){
this.current = arg0;
this.goTo = arg1;
}
@Override
public void onClick(View v) {
Intent intent = new Intent(current, goTo);
}
}
and now i want to go to next activity when button click’s but when i put this line startActivity(intent); compiler shows error The method startActivity(Intent) is undefined for the type OnClickListener
I dont know where i am going wrong and what i am missing. Please help me to solve this problem
You can do in another way too, let your class implement the
OnClickListenerinterface and then implementonClick(...)method, an in that method, switch on view’s id like this: