android - How to start a method with a button click -


i trying start method clicking button. idea, , please tell me if there better way, put code calculations method called calculateratios. once button clicked run through if statements check , see if of textfields empty. if none empty runs method calculateratios. otherwise display toast saying "enter data". have following java.

java file

package com.th3ramr0d.poundforpound;  import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import org.w3c.dom.text;  public class mainactivity extends appcompatactivity {  button calculate; button reset;  edittext inputname1; edittext inputname2; edittext inputbodyweight1; edittext inputweightlifted1; edittext inputbodyweight2; edittext inputweightlifted2;   textview outputname1; textview outputname2; textview outputratio1; textview outputratio2; textview finaloutput;  double inputbodyweight1var = 0; double inputweightlifted1var = 0; double inputbodyweight2var = 0; double inputweightlifted2var = 0; double ratio1 = 0; double ratio2 = 0;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      calculate = (button)findviewbyid(r.id.calculate);     reset = (button)findviewbyid(r.id.reset);      inputname1 = (edittext)findviewbyid(r.id.inputname1);     inputname2 = (edittext)findviewbyid(r.id.inputname2);     inputbodyweight1 = (edittext)findviewbyid(r.id.inputbodyweight1);     inputweightlifted1 = (edittext)findviewbyid(r.id.inputweightlifted1);     inputbodyweight2 = (edittext)findviewbyid(r.id.inputbodyweight2);     inputweightlifted2 = (edittext)findviewbyid(r.id.inputweightlifted2);      outputname1 = (textview)findviewbyid(r.id.outputname1);     outputname2 = (textview)findviewbyid(r.id.outputname2);     outputratio1 = (textview)findviewbyid(r.id.outputratio1);     outputratio2 = (textview)findviewbyid(r.id.outputratio2);     finaloutput = (textview)findviewbyid(r.id.finaloutput);       reset.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v){             inputname1.settext("");             inputname2.settext("");             inputbodyweight1.settext("");             inputbodyweight2.settext("");             inputweightlifted1.settext("");             inputweightlifted2.settext("");             outputname1.settext("");             outputname2.settext("");             outputratio1.settext("");             outputratio2.settext("");             finaloutput.settext("");          }      }     );  // // //this want click button check if statements // //     calculate.setonclicklistener(             new view.onclicklistener() {                 @override                 public void onclick(view v) {                 calculateratios();                   }             }     );  }  //this method want run if ifstatements allow  public void caculateratios(){     string inputname1var = inputname1.gettext().tostring();     string inputname2var = inputname2.gettext().tostring();      inputbodyweight1var = integer.parseint(inputbodyweight1.gettext().tostring());     inputweightlifted1var = integer.parseint(inputweightlifted1.gettext().tostring());     inputbodyweight2var = integer.parseint(inputbodyweight2.gettext().tostring());     inputweightlifted2var = integer.parseint(inputweightlifted2.gettext().tostring());       double ratio1 = inputweightlifted1var / inputbodyweight1var;     double ratio2 = inputweightlifted2var / inputbodyweight2var;      outputname1.settext(inputname1var);     outputname2.settext(inputname2var);      outputratio1.settext(string.format("%.2f" + ": 1", ratio1));     outputratio2.settext(string.format("%.2f" + ": 1", ratio2));      if (ratio1 > ratio2){         double strengthdif = ratio2 / ratio1;         strengthdif = (1 - strengthdif) * 100;         finaloutput.settext(inputname1var + " stronger " + inputname2var + " " + (string.format("%.2f", strengthdif)) + "%");     }     else{         double strengthdif = ratio1 / ratio2;         strengthdif = (1 - strengthdif) * 100;          finaloutput.settext(inputname2var + " stronger " + inputname1var + " " + (string.format("%.2f", strengthdif)) + "%");     } };  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } 

}

the problem unsure of how method. when set above calculateratios() called in onclick red , logcat says:

error:(87, 21) error: cannot find symbol method calculateratios()

any on how set properly? thanks!

the method name invoking in button click method not match method name trying invoke... honest don't know how code compiles if using ide. change onclick following:

calculate.setonclicklistener(         new view.onclicklistener() {             @override             public void onclick(view v) {             //old:   calculateratios();             /*new:*/ caculateratios();             }         } ); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -