1. Are you ready for the Galaxy S20? Here is everything we know so far!

Help on my app?

Discussion in 'Android Development' started by jtrain45, Aug 15, 2011.

  1. jtrain45

    jtrain45 Lurker
    Thread Starter

    Hello, I'm not sure if this is in the right place, I am new here. Anyway, I need help and advice for my app. I have created(for the most part) a calculator that solves a quadratic equation based on the three numbers the user inputs. I have a calculate button that I want to display the answer when it gets clicked and after it does the calculations. It doesn't display anything. I also have a reset button that will reset the text fields but same problem. I will put my code below this. If you see any other errors please tell me. Thanks
    Code (Text):
    1. [CODE]
    package android.jackson.com;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnKeyListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class myMenu extends Activity {

    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private Button calcbutton;
    private Button resetbutton;
    private TextView textView6; // Answer
    private OnKeyListener mKeyListener;

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /** Called when the activity is first created. */
    //reset button will be a menu option
    editText1 = (EditText) findViewById(R.id.editText1);
    editText1.requestFocus();
    editText1.setOnKeyListener(mKeyListener);
    editText2 = (EditText) findViewById(R.id.editText2);
    editText3 = (EditText) findViewById(R.id.editText3);
    resetbutton = (Button) findViewById(R.id.resetbutton);
    calcbutton = (Button) findViewById(R.id.calcbutton);




    textView6 = (TextView) findViewById(R.id.textView6);


    }


    private OnClickListener mKeylistener = new OnClickListener(){
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId()== R.id.calcbutton){
    calculate();





    }
    else {
    reset();
    }




    }

    private void reset() {
    // TODO Auto-generated method stub
    editText1.setText("");
    editText2.setText("");
    editText3.setText("");
    editText1.requestFocus();


    }

    private void calculate() {
    // TODO Auto-generated method stub
    //****QUADRATIC FORMULA HERE**************
    textView6 = (TextView) findViewById(R.id.textView6);
    double A= Double.parseDouble(editText1.getText().toString());
    double B= Double.parseDouble(editText2.getText().toString());
    double C= Double.parseDouble(editText3.getText().toString());
    double root1;
    double root2;
    double discriminant;

    A= Double.parseDouble(editText1.getText().toString());
    B = Double.parseDouble(editText2.getText().toString());
    C = Double.parseDouble(editText3.getText().toString());

    discriminant = Math.sqrt(B*B-4*A*C);

    if(discriminant >0) {

    textView6.setText("No roots");

    }

    else {
    root1 = (-B + discriminant) / (2*A);
    root2 = (-B - discriminant) / (2*A);

    }
    // *******should be showing answer***********
    textView6.setText(root1 + "and" + root2);

    };




    public boolean onCreateOptionsMenu(android.view.Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater awesome = getMenuInflater();
    awesome.inflate(R.menu.main_menu, menu);
    return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuSweet:
    startActivity(new Intent("android.jackson.com.SWEET"));
    return true;
    case R.id.menuToast:
    Toast andEggs = Toast.makeText(myMenu.this, "Developed by Jackson Hughes",
    Toast.LENGTH_LONG);
    andEggs.show();
    return true;

    }

    return false;
    }

    };
    }
    Code (Text):
    1. [CODE]
    [/CODE]
     

    Advertisement

  2. AccelArlene

    AccelArlene Newbie

  3. qasimValent

    qasimValent Newbie

    you need your buttons to have onclicklisteners set on them,


    e.g.



    resetbutton
    .setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {

    // reset code in here

    }

    }) ;




    calcbutton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {


    // calculate code in here

    }

    }) ;



     
Loading...
Similar Threads - Help app
  1. Abraham Mustri
    Replies:
    0
    Views:
    127
  2. jthompff
    Replies:
    2
    Views:
    255
  3. CindysPhonesare4calls
    Replies:
    6
    Views:
    440
  4. Android News
    Replies:
    0
    Views:
    71
  5. Android News
    Replies:
    0
    Views:
    62
  6. Android News
    Replies:
    0
    Views:
    33
  7. Victor001
    Replies:
    1
    Views:
    358
  8. psixichka
    Replies:
    3
    Views:
    438
  9. Aseem Chishti
    Replies:
    0
    Views:
    194

Share This Page

Loading...