I'm currently making a simple calculator app on Android. Im trying to set up the code so that when a number button is pressed it updates the calculator screen with that number. Currently I'm doing it like this.
Button one = (Button) findViewById(R.id.oneButton); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView output = (TextView) findViewById(R.id.output); output.append("1"); } });
It works but I'm writing this same code for every single button on the calculator. As you can imagine it is very redundant. Is there anyway I can write this code in a more efficient way? One that involves not writing this method for every single button?
This is no longer possible,android lint checks prevent you from adding same ids in the same xml file. A work-around would be to use the include tag to include other xmls with repeating ids. Nevertheless this is an approach that should be avoided.
Show activity on this post. call setClickable(false) for all buttons once one of them was clicked. call the next activity with startActivityForResult(...) override onActivityResult(...) and call setClickable(true) for all buttons inside it.
Answer. As HTML and CSS are designed to be very fault tolerant, most browsers will in fact apply the specified styles to all elements given the same id. However, this is considered bad practice as it defies the W3C spec. Applying the same id to multiple elements is invalid HTML and should be avoided.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
You Just Simply have to Follow these steps for making it easy...
You don't have to write new onClickListener
for Every Button
... Just Implement View.OnClickLister
to your Activity
/Fragment
.. it will implement new Method called onClick()
for handling onClick Events for Button
,TextView` etc.
- Implement
OnClickListener()
in yourActivity
/Fragment
public class MainActivity extends Activity implements View.OnClickListener { }
- Implement onClick() method in your Activity/Fragment
public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onClick(View v) { // default method for handling onClick Events.. } }
- Implement
OnClickListener()
For Buttons
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout); Button one = (Button) findViewById(R.id.oneButton); one.setOnClickListener(this); // calling onClick() method Button two = (Button) findViewById(R.id.twoButton); two.setOnClickListener(this); Button three = (Button) findViewById(R.id.threeButton); three.setOnClickListener(this); }
- Find Buttons By Id and Implement Your Code..
@Override public void onClick(View v) { switch (v.getId()) { case R.id.oneButton: // do your code break; case R.id.twoButton: // do your code break; case R.id.threeButton: // do your code break; default: break; } }
Please refer to this link for more information :
https://androidacademic.blogspot.com/2016/12/multiple-buttons-onclicklistener-android.html (updated)
This will make easier to handle many buttons click events and makes it looks simple to manage it...
You could set the property:
android:onClick="buttonClicked"
in the xml file for each of those buttons, and use this in the java code:
public void buttonClicked(View view) { if (view.getId() == R.id.button1) { // button1 action } else if (view.getId() == R.id.button2) { //button2 action } else if (view.getId() == R.id.button3) { //button3 action } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With