Learn about how to set an on-click listener for multiple buttons on the Android
Photo by Gal Shir |
onClickListener()
for
multiple views in Activity, Fragments or
RecyclerView. But making it easier to implement and handle is a different task. For some
reason, many people try to implement multiple
onClickListener()
for individual views. But you can implement the
interface to handle it easily. You don't have to write a new
onClickListener
for every button. Just Implement
View.OnClickListner
to your Activity/Fragment
. It
will add a new method to your class called onClick()
for handling
click events for Button, TextView, etc. You Just Simply have to Follow these steps to make it easy.
Step 1: Implement OnClickListener()
interface in your
Activity/Fragment
public class MainActivity extends AppCompatActivity implements View.OnClickListener { // TODO }
After implementing an interface to your
Activity/Fragment
it
will ask you to implement its method onClick()
.
Step 2: Implement onClick() method in your Activity/Fragment
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override public void onClick(View v) { // handling onClick Events } }
onClick()
method to your
class. It is the default method called when onClick
is called
from view. It means that when the user clicks on your view, your
class will redirect calls to this method.
Step 3: Implement OnClickListener() For Buttons
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout); Button one = (Button) findViewById(R.id.buttonOne); one.setOnClickListener(this); // calling onClick() method Button two = (Button) findViewById(R.id.buttonTwo); two.setOnClickListener(this); Button three = (Button) findViewById(R.id.buttonThree); three.setOnClickListener(this); } @Override public void onClick(View v) { } }
this
keyword to the
onClickListener()
method. It redirects the call to the
onClick()
method. If you don't use this keyword here, your call
will not be redirected to the onClick() method when a user performs a click.
Step 4: Find Buttons by ID and Implement Your Code for the button click.
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonOne: // code for button when user clicks buttonOne. break; case R.id.buttonTwo: // do your code break; case R.id.buttonThree: // do your code break; default: break; } } }
onClick
calls from one place and it will help you to make your code
look clean and more readable.
Would u please do one with just two buttons to set on different activites
ReplyDeleteYou can use same type of implementation for both activities. Implement `OnClickListener` interface in activity/fragment. Override `onClick()` method, and assign that listener with `setOnClickListener()` method of buttons.
Deletewhat can i do if i want to do same thing with 3 button . this code dose not work for same thing
ReplyDeleteuse same method or call for those 3 buttons by passing same case into switch.
Deletecase R.id.buttonOne:
case R.id.buttonTwo:
case R.id.buttonThree:
// code for button when user clicks buttonOne.
break;
Perfect!
ReplyDeleteI Get java. Lang. Runtime exception
ReplyDeletecan you be provide more info so I can help?
DeleteI have an error that says "Constant expression required." What can I do about this?
ReplyDelete