title: two buttons with the same "onClick" - Android
body: <p>I am trying out Android. Initially I had this:</p>
  
  <p><img src="https://i.stack.imgur.com/V7CGC.png" alt="original plan"></p>
  
  <p>I created an extra button called "Reset". I lined them up in a row by using the layout Table Row, which looks like the following:</p>
  
  <p><img src="https://i.stack.imgur.com/xqiKY.png" alt="enter image description here"></p>
  
  <p>The "Calculate" has been assigned to the string "clickHandler", and its clickable property is also assigned to "clickHandler".
  I basically got this tutorial off here
  <a href="http://www.vogella.de/articles/Android/article.html#first" rel="nofollow noreferrer">Tutorial site</a></p>
  
  <p>So far my code looks like this</p>
  
  <pre><code>package ywong02.android.temperature;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.RadioButton;
  import android.widget.Toast;
  import android.widget.Button;
  
  public class Convert extends Activity {
      /** Called when the activity is first created. */
  
      private EditText text;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          text = (EditText)findViewById(R.id.editText1);
      }
  
      public void myClickHandler(View view){
  
          RadioButton celsiusButton = (RadioButton)findViewById(R.id.radio0);
          RadioButton fahrenheitButton = (RadioButton)findViewById(R.id.radio1);
          Button resetButton = (Button)findViewById(R.id.button2);
  
          switch(view.getId()){       
          case R.id.button1:
              if(text.getText().length() == 0){
                  Toast.makeText(this, "Please enter a number to convert", Toast.LENGTH_LONG).show();
                  return;
              }
              float inputValue = Float.parseFloat(text.getText().toString());
  
                      // second attempt: make Reset button onClick to False initially
                      // resetButton.isClickable();
  
              if(celsiusButton.isChecked()){
                  text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
              }
              else{
                  text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
              }
              // switch to the other button
              if(fahrenheitButton.isChecked()){
                  fahrenheitButton.setChecked(false);
                  celsiusButton.setChecked(true);
              }
              else {
                  fahrenheitButton.setChecked(true);
                  celsiusButton.setChecked(false);
              }
              break;  // don't forget to break at each case!!!
          case R.id.button2:  // reset button
              fahrenheitButton.setChecked(false);
              celsiusButton.setChecked(true);
              //text.setText("");
              //text.setText(null);
              break;
  
          }
          return;
      }
  
      private float convertFahrenheitToCelsius(float fahrenheit) {}
      private float convertCelsiusToFahrenheit(float celsius) {}
  }
  </code></pre>
  
  <p>The first attempt:</p>
  
  <ol>
  <li>(1) assign both buttons clickable to
  Default (from properties) 
  [main.xml]</li>
  <li>(2) with and without the setText in
  the the second switch case</li>
  </ol>
  
  <p>Result:  Error Msg from Android  --> Application was forced to stopped</p>
  
  <p>Second attempt:</p>
  
  <ol>
  <li>assign the reset button's clickable property to False </li>
  <li>add the extra
  resetButton.isClickable() to the
  java source code</li>
  <li>with / without the setText </li>
  </ol>
  
  <p>Result:  Reset has no effect (I can't even click on
      it. it doesn't seem like it is
      clickable!!)</p>
  
  <p><strong></strong>
  My real problem is to how to get the reset button working.</p>
  
  <p>Is it wrong to assign both button to the same "onClick"  (in this case, it is called myclickHandler)?? </p>
  
  <p>If the answer to the previous question is YES, then I must create two separate classes, one handles the reset, and the other handles the calculate button. Am I right??</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>Update (posting XML - this one assign both buttons to Clickable (Default))</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@color/bgColor"&gt;
  &lt;EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal|numberSigned"&gt;&lt;/EditText&gt;
  &lt;RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
      &lt;RadioButton android:text="@string/toCelsius" android:layout_width="wrap_content" android:id="@+id/radio0" android:checked="true" android:layout_height="wrap_content"&gt;&lt;/RadioButton&gt;
      &lt;RadioButton android:text="@string/toFahrenheit" android:layout_width="wrap_content" android:id="@+id/radio1" android:layout_height="wrap_content"&gt;&lt;/RadioButton&gt;
  &lt;/RadioGroup&gt;
  &lt;TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content"&gt;
      &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:onClick="myClickHandler" android:text="@string/pressToCalc" android:layout_height="wrap_content"&gt;&lt;/Button&gt;
      &lt;Button android:layout_height="wrap_content" android:id="@+id/button2" android:layout_width="wrap_content" android:onClick="myclickHandler" android:text="@string/clickToReset"&gt;&lt;/Button&gt;
  &lt;/TableRow&gt;
  &lt;/LinearLayout&gt;
  </code></pre>