Reputation: 1624
I started program little bit in android, I have 3 buttons in a single activity.
I saw some example codes that assign the same OnClick
event to all the buttons (even if they perform completely different action) and in the method Switch(id)
case case case...
What is the better approach? one onClick
method and switching or a lot of methods, one for each button?
Thanks.
Upvotes: 28
Views: 86403
Reputation: 26071
Little addition to @Nguyen answer.
findViewById(R.id.buttonOne).setOnClickListener(buttonClickListener);
.... .... .... ....
findViewById(R.id.buttonN).setOnClickListener(buttonClickListener);
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonOne:
// do something
break;
.... .... .... ....
case R.id.buttonN:
// do something
break;
}
}
};
This could be useful, if you don't want to initialize the button variable, but want to track button click event. Thanks!
Upvotes: 0
Reputation: 109237
If you want to reduce the coding lines then use View's OnClick() with switch statement
and if you want to handle separately all click (for easily understanding and maintaining code) then use separate all button's onClick().
Update:
If you have declared Buttons in your Activity layout xml file, than write attribute android:onClick=""
with same method name for all buttons and implement that method in your activity. Now you have one method for all buttons and in that method differentiate buttons with id.
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:onClick="buttonOnClick"
android:layout_height="wrap_content"
android:text="Hello, I am a Button 1" />
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:onClick="buttonOnClick"
android:layout_height="wrap_content"
android:text="Hello, I am a Button 2" />
<Button android:id="@+id/button3"
android:layout_width="wrap_content"
android:onClick="buttonOnClick"
android:layout_height="wrap_content"
android:text="Hello, I am a Button 3" />
</LinearLayout>
Now in your Activity implement buttonOnClick
like,
public void buttonOnClick(View view)
{
switch(view.getId())
{
case R.id.button1:
// Code for button 1 click
break;
case R.id.button2:
// Code for button 2 click
break;
case R.id.button3:
// Code for button 3 click
break;
}
}
Or you can apply same switch case for dynamically added buttons in your activity,
like instead of buttonOnClick
you have to use implemented View's OnClickListerner's onClick
.
Upvotes: 29
Reputation: 34360
Registered onClick event in the XML layout and then handle it in the code. This is how I would do it:
<Button
android:id="@+id/btplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="onBtnClicked">
Method in .class
public void onBtnClicked(View v) {
switch (v.getId()) {
case R.id.btplus:
Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show();
break;
case R.id.btminu:
Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
Upvotes: 0
Reputation: 24423
Use this way:
@Override
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
button3.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
//DO something
break;
case R.id.button2:
//DO something
break;
case R.id.button3:
//DO something
break;
}
}
};
Upvotes: 35
Reputation: 1022
this.btnAddFriedtoFacebook = (Button) this.findViewById(R.id.btnAddFriedtoFacebook);
this.btnAddFriedtoFacebook.setOnClickListener(this.backButtonClickListener);
public OnClickListener backButtonClickListener = new OnClickListener()
{
public void onClick(final View view)
{
if (view == MatchInfoActivity.this.btnBack)
{
MatchInfoActivity.this.finish();
}
if( view == MatchInfoActivity.this.btnAddFried){
Intent i = new Intent(Intent.ACTION_VIEW);
MatchInfoActivity.this.startActivity(i);
}
if( view == MatchInfoActivity.this.btnAddBuddy){
Intent i = new Intent(Intent.ACTION_VIEW);
MatchInfoActivity.this.startActivity(i);
}
}
};
Here is the good way.
Upvotes: 6
Reputation: 94625
I think registering onClick
in xml (layout) is better approach.
EDIT:
Found related threads :
Upvotes: 2