• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help on my app?

jtrain45

Lurker
Aug 14, 2011
1
0
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:
[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:
[CODE]
[/CODE]
 
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

}

}) ;



 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones