So hello guys today we are entirely discussing how to add onClickListener or onClickEvent on android studio. Using this onClickListener in the android studio you can go from one activity to another. There are straightforward steps that you should follow. 


After you can use these simple steps to add an on-click listener in the android app project. Using this given method you can easily apply an on-click listener in your views like button, text view, and image view. So let's get started. 



First, you need to add the views like buttons, images, and text in your layout.xml file, Then give these view IDs.


How to add onclicklistener in android studio for one activity to another activity


now we want to go on the .java file and initialize views and give findviewbyid all the given views in your layout,XML file.


How to add onclicklistener in android studio for one activity to another activity


now after we are ready to set the function of onClickListener in these different views with the simple steps and there you need to type the below code

textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Textview click", Toast.LENGTH_SHORT).show();

}
});

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Click", Toast.LENGTH_SHORT).show();
}
});

imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Imageview clicked", Toast.LENGTH_SHORT).show();
}
});



This is given above of the code that is used for the setting that your on-click listener is clicked, now there we are sharing with you the full method that how to go from one activity to another using an on-click listener.


These are also straightforward steps that you should follow to add this function to go from one activity to another.


To go to another activity you need to create another activity, when you create a new activity or which existing created activity you want to do then you just follow these below steps.


textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Textview click", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);

}
});

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Click", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});

imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Imageview clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});




These are given the method that can use to go from one activity to another activity using Intent in android studio. If you like this code then please share it with your friends and comment me below if any problems that you face. I will give you a complete solution with a full explanation 


Thanks for reading 

By About For All