I'm new to android development and having a hard time trying to call the appropriate function on my setOnClickListener, I have 3 function each have a set of editText that the user can input some double values this values are then return by the function and when the user clicks calculate displays the right information, however, I'm trying to check if there's a value either in the editText or the function so I can display the right information, but I can't figure it out.
1 function at a time works
public class MainWindowActivity extends AppCompatActivity {
private Button calculateButton;
private EditText length1Text;
private EditText length2Text;
private EditText length3Text;
private EditText width1Text;
private EditText width2Text;
private EditText width3Text;
private EditText thick1Text;
private EditText thick2Text;
private EditText thick3Text;
private EditText squareYards1Text;
private EditText squareYards2Text;
private EditText squareYards3Text;
private EditText result1TotalText;
private EditText result2TotalText;
private EditText result3TotalText;
private EditText grandTotalText;
private Double length1 = 0.0;
private Double length2 = 0.0;
private Double length3 = 0.0;
private Double width1 = 0.0;
private Double width2 = 0.0;
private Double width3 = 0.0;
private Double thick1 = 0.0;
private Double thick2 = 0.0;
private Double thick3 = 0.0;
private Double syard1 = 0.0;
private Double syard2 = 0.0;
private Double syard3 = 0.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_window);
calculateButton = (Button) findViewById(R.id.calculateButtonPressed);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(CalculateArea1() == null){
CalculateArea2();
CalculateArea3();
Total = CalculateArea2() + CalculateArea3();
}
grandTotalText.setText(String.format("%.1f",Total));
}
});
public Double CalculateArea1(){
length1Text = (EditText) findViewById(R.id.length1Text);
width1Text = (EditText) findViewById(R.id.width1Text);
thick1Text = (EditText) findViewById(R.id.thickness1Text);
squareYards1Text = (EditText) findViewById(R.id.squareYards1Text);
result1TotalText = (EditText) findViewById(R.id.result1TotalText);
grandTotalText = (EditText) findViewById(R.id.grandTotalText);
if (squareYards1Text.getText().toString().trim().length() == 0) {
length1 = Double.valueOf(length1Text.getText().toString());
width1 = Double.valueOf(width1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}else{
syard1 = Double.valueOf(squareYards1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}
//area 1
double yard = length1 * width1 / 9;
double t = yard / thick1;
double y1 = 12.20 / thick1;
double sy1 = syard1 / y1;
if (thick1 == 1) {
t = yard / 12.20;
}else if(thick1 == 1.25){
t = yard / 9.76;
}else if (thick1 == 1.5){
t = yard / 8.13;
}else if (thick1 == 1.75){
t = yard / 6.97;
}else if (thick1 == 2){
t = yard / 6.1;
}
Double result;
if (length1Text.getText().toString().trim().length() == 0) {
result1TotalText.setText(String.format("%.1f", sy1));
grandTotalText.setText(String.format("%.1f", sy1));
result = sy1;
} else {
result1TotalText.setText(String.format("%.1f", t));
squareYards1Text.setText(String.format("%.1f", yard));
grandTotalText.setText(String.format("%.1f", t));
result = t;
}
return result;
}
public Double CalculateArea2(){
length2Text = (EditText) findViewById(R.id.length2Text);
width2Text = (EditText) findViewById(R.id.width2Text);
thick2Text = (EditText) findViewById(R.id.thickness2Text);
squareYards2Text = (EditText) findViewById(R.id.squareYards2Text);
result2TotalText = (EditText) findViewById(R.id.result2TotalText);
grandTotalText = (EditText) findViewById(R.id.grandTotalText);
if (squareYards2Text.getText().toString().trim().length() == 0) {
length2 = Double.valueOf(length2Text.getText().toString());
width2 = Double.valueOf(width2Text.getText().toString());
thick2 = Double.valueOf(thick2Text.getText().toString());
}else{
syard2 = Double.valueOf(squareYards2Text.getText().toString());
thick2 = Double.valueOf(thick2Text.getText().toString());
}
//area 2
double yard2 = length2 * width2 / 9;
double t2 = yard2 / thick2;
double y2 = 12.20 / thick2;
double sy2 = syard2 / y2;
if (thick2 == 1) {
t2 = yard2 / 12.20;
}else if(thick2 == 1.25){
t2 = yard2 / 9.76;
}else if (thick2 == 1.5){
t2 = yard2 / 8.13;
}else if (thick2 == 1.75){
t2 = yard2 / 6.97;
}else if (thick2 == 2){
t2 = yard2 / 6.1;
}
Double result;
if (length2Text.getText().toString().trim().length() == 0) {
result2TotalText.setText(String.format("%.1f", sy2));
grandTotalText.setText(String.format("%.1f", sy2));
result = sy2;
} else {
result2TotalText.setText(String.format("%.1f", t2));
squareYards2Text.setText(String.format("%.1f", yard2));
grandTotalText.setText(String.format("%.1f", t2));
result = t2;
}
return result;
}
public Double CalculateArea3(){
length3Text = (EditText) findViewById(R.id.length3Text);
width3Text = (EditText) findViewById(R.id.width3Text);
thick3Text = (EditText) findViewById(R.id.thickness3Text);
squareYards3Text = (EditText) findViewById(R.id.squareYards3Text);
result3TotalText = (EditText) findViewById(R.id.result3TotalText);
grandTotalText = (EditText) findViewById(R.id.grandTotalText);
if (squareYards3Text.getText().toString().trim().length() == 0) {
length3 = Double.valueOf(length3Text.getText().toString());
width3 = Double.valueOf(width3Text.getText().toString());
thick3 = Double.valueOf(thick3Text.getText().toString());
}else{
syard3 = Double.valueOf(squareYards3Text.getText().toString());
thick3 = Double.valueOf(thick3Text.getText().toString());
}
//area 3
double yard3 = length3 * width3 / 9;
double t3 = yard3 / thick3;
double y3 = 12.20 / thick3;
double sy3 = syard3 / y3;
if (thick3 == 1) {
t3 = yard3 / 12.20;
}else if(thick3 == 1.25){
t3 = yard3 / 9.76;
}else if (thick3 == 1.5){
t3 = yard3 / 8.13;
}else if (thick3 == 1.75){
t3 = yard3 / 6.97;
}else if (thick3 == 2){
t3 = yard3 / 6.1;
}
Double result;
if (length3Text.getText().toString().trim().length() == 0) {
result3TotalText.setText(String.format("%.1f", sy3));
grandTotalText.setText(String.format("%.1f", sy3));
result = sy3;
} else {
result3TotalText.setText(String.format("%.1f", t3));
squareYards3Text.setText(String.format("%.1f", yard3));
grandTotalText.setText(String.format("%.1f", t3));
result = t3;
}
return result;
}
}
I just did the check on the first to keep it short.
Any help will be greatly appreciated.
Give us your code so we can help, I need to see your CalculateArea{1,2,3}.
Okay mate, I think I find the solution and it is really easy. When you write OnClick() function like that you need to assign OnClick function on XML file as well so the button knows that it has a "function" to do something.
Here is an example ;
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="OnClick" />
Hope it will be helpful.
if (squareYards1Text.getText().toString().trim().length() == 0) {
length1 = Double.valueOf(length1Text.getText().toString());
width1 = Double.valueOf(width1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}else{
syard1 = Double.valueOf(squareYards1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
// Toast.makeText(MainWindowActivity.this, "inside the 'ELSE' statement", Toast.LENGTH_LONG).show();
}
the code above was the problem, I had to do one more check and change the values if the condition was true like this.
if (squareYards1Text.getText().toString().trim().length() == 0 && length1Text.getText().toString().trim().length() == 0) {
length1 = 0.0;
syard1 = 0.0;
width1 = 0.0;
}else if(squareYards1Text.getText().toString().trim().length() == 0){
length1 = Double.valueOf(length1Text.getText().toString());
width1 = Double.valueOf(width1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}else{
syard1 = Double.valueOf(squareYards1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}
Now it doesn't matter if only one area is being use or 2 or 3 or all of them, it will never find an "invalid double". I had to do this for all areas.
Thanks.
Related
`So i've been having a problem with my code, im currently trying to put 2 events on a single button. But it only allows one event at a time
public void event1(View view) {
boolean retorno = true;
String c1 = et1.getText().toString();
String c2 = et2.getText().toString();
String c3 = et3.getText().toString();
if (c1.isEmpty()) {
retorno = false;
}
if (c2.isEmpty()) {
retorno = false;
}
if (c3.isEmpty()) {
retorno = false;
}
}
public void event2(View view) {
double num1, num2, num3;
double MlGrama, LKG;
String select;
num1 = parseDouble(et1.getText().toString());
num2 = parseDouble(et2.getText().toString());
num3 = parseDouble(et3.getText().toString());
select = sp1.getSelectedItem().toString();
if (select.equals("Ml/Grama")) {
MlGrama = (num1 / num2) * num3;
DecimalFormat df = new DecimalFormat(".00");
tv3.setText("O resultado é: " + df.format(MlGrama));
} else {
if (select.equals("Litro/Kg")) {
LKG = ((num1 * 1000) * num2) * num3;
DecimalFormat df = new DecimalFormat(".00");
tv3.setText("O resultado é: " + df.format(LKG));
}
}
}
android:onClick="event1, event2"
I tried using setOnClickListener, but it only makes one of the two actions.
I also tried using the 2 actions in one void, but it also does not work.
If you want to call 2 different events in one action why don't you put them one after another like this:
public void onButtonClicked(View view) {
event1(view);
event2(view);
}
android:onClick="onButtonClicked"
I create alist method to insert principal values and remaining balances according to the payments.But when I implement this method in actionListner method error occurs(EventDispatchThread Execption).I try to solve it.Please give any help for me to solve this.
public class Loan {
float lamt,irate,inst,lbalance;
int duration,nop;
String amtlist[][] = new String[duration+1][5];
Loan(float lamt, float irate, int duration, int nop) {
this.lamt = lamt;
this.irate = irate;
this.duration = duration;
this.nop = nop;
}
public void alist() {
amtlist[0][0] = "0" ;
amtlist[0][1] = "0";
amtlist[0][2] = "0";
amtlist[0][3] = "0";
amtlist[0][4] = Float.toString(inst*annuity(duration,irate));
float v = 1 / (1 + irate);
for (int i = 1; i < duration + 1; i++) {
amtlist[i][0] = Float.toString(i);
amtlist[i][1] = Float.toString(irate * annuity(duration + 1 - i, irate));
amtlist[i][2] = Float.toString(instalment());
amtlist[i][3] = Double.toString(instalment() * Math.pow(v, i));
amtlist[i][4] = Float.toString(instalment() * annuity(duration - i, irate));
}
}
public float annuity(int duration, float irate) {
float v = 1 / (1 + irate);
float an = (float) ((1 - Math.pow(v, duration)) / irate);
return an;
}
public float instalment(){
return inst = lamt / annuity(duration, irate);
}
public float lbalance() {
return lbalance = inst * annuity(duration - nop, irate);
}
}
public void actionPerformed(ActionEvent e) {
String lamtstr = tf1.getText();
String iratestr = tf2.getText();
String durationstr = tf3.getText();
String nopstr = tf4.getText();
float lamt = Float.parseFloat(lamtstr);
float irate = Float.parseFloat(iratestr);
int duration = Integer.parseInt(durationstr);
int nop = Integer.parseInt(nopstr);
Loan l = new Loan(lamt, irate, duration, nop);
float inst = 0, balance = 0;
if (e.getSource() == b1) {
inst =l.instalment();
balance =l.lbalance();
}
if (e.getSource() == b2) {
l.alist();
fscr.dispose();
new AmTable(l.amtlist);
}
tf5.setText(String.valueOf(inst));
tf6.setText(String.valueOf(balance));
}
I tried solve it by changing alist method to value returning method but my attempt got vain.
if (e.getSource() == b2) {
l.alist();
fscr.dispose();
new AmTable(l.amtlist);
}
I expected to call AmTable function with amlist array.
I am trying to create an app that requires a login. Just to test to see how it works, I made a login button to go to a separate layout where you press a button to randomize numbers. The login button works fine and it brings me to another layout, but it is when I click the button on the separate layout that my app crashes.
here is my code:
private TextView R1;
private TextView R2;
private TextView R3;
private TextView R4;
private TextView R5;
private TextView R6;
private Animation R1_roll_rotate;
private Animation R2_roll_rotate;
private Animation R3_roll_rotate;
private Animation R4_roll_rotate;
private Animation R5_roll_rotate;
private Animation R6_roll_rotate;
private Button Randomize_Button;
private Animation Button_Rotate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Randomize_Button= (Button) findViewById(R.id.Randomize);
R1= (TextView) findViewById(R.id.RandomOne);
R2= (TextView) findViewById(R.id.RandomTwo);
R3= (TextView) findViewById(R.id.RandomThree);
R4= (TextView) findViewById(R.id.RandomFour);
R5= (TextView) findViewById(R.id.RandomFive);
R6= (TextView) findViewById(R.id.RandomSix);
Button_Rotate= AnimationUtils.loadAnimation(Main_Activity.this, R.anim.Button_Rotate);
R1_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomOne_roll_rotate);
R2_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomTwo_roll_rotate);
R3_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomThree_roll_rotate);
R4_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFour_roll_rotate);
R5_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFive_roll_rotate);
R6_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomSix_roll_rotate);
public void onClick(View view) {
int randomnum1 = r.nextInt(70);
int randomnum2 = r.nextInt(70);
int randomnum3 = r.nextInt(70);
int randomnum4 = r.nextInt(70);
int randomnum5 = r.nextInt(70);
int randomnum6 = r.nextInt(27);
int num1 = randomnum1;
int num2 = randomnum2;
int num3 = randomnum3;
int num4 = randomnum4;
int num5 = randomnum5;
int num6 = randomnum6;
String randomnum7 = String.valueOf(randomnum1);
String randomnum8 = String.valueOf(randomnum2);
String randomnum9 = String.valueOf(randomnum3);
String randomnum10 = String.valueOf(randomnum4);
String randomnum11 = String.valueOf(randomnum5);
String randomnum12 = String.valueOf(randomnum6);
try {
Randomize_Button.startAnimation(Button_Rotate);
R1.startAnimation(R1_roll_rotate);
R2.startAnimation(R2_roll_rotate);
R3.startAnimation(R3_roll_rotate);
R4.startAnimation(R4_roll_rotate);
R5.startAnimation(R5_roll_rotate);
R6.startAnimation(R6_roll_rotate);*/
for (int i = num1; i > 0 && i == num1; i++) {
String ii = Integer.toString(i);
R1.setText(ii);
}
for (int j = num2; j > 0 && j == num2; j++) {
String jj = Integer.toString(j);
R2.setText(jj);
}
for (int k = num3; k > 0 && k == num3; k++) {
String kk = Integer.toString(k);
R3.setText(kk);
}
for (int d = num4; d > 0 && d == num4; d++) {
String dd = Integer.toString(d);
R4.setText(dd);
}
for (int s = num5; s > 0 && s == num5; s++) {
String ss = Integer.toString(s);
R5.setText(ss);
}
for (int g = num6; g > 0 && g == num6; g++) {
String gg = Integer.toString(g);
R6.setText(gg);
}
for (int g = num1; g == num2; g++) {
g = r.nextInt(70);
String gg = Integer.toString(g);
R1.setText(gg);
}
for (int d = num1; d == num3; d++) {
d = r.nextInt(70);
String dd = Integer.toString(d);
R1.setText(dd);
}
for (int a = num1; a == num3; a++) {
a = r.nextInt(70);
String aa = Integer.toString(a);
R1.setText(aa);
}
for (int w = num1; w == num4; w++) {
w = r.nextInt(70);
String ww = Integer.toString(w);
R1.setText(ww);
}
for (int q = num1; q == num5; q++) {
q = r.nextInt(70);
String qq = Integer.toString(q);
R1.setText(qq);
}
for (int y = num2; y == num3; y++) {
y = r.nextInt(70);
String yy = Integer.toString(y);
R2.setText(yy);
}
for (int o = num2; o == num4; o++) {
o = r.nextInt(70);
String oo = Integer.toString(o);
R2.setText(oo);
}
for (int p = num2; p == num5; p++) {
p = r.nextInt(70);
String pp = Integer.toString(p);
R2.setText(pp);
}
for (int x = num3; x == num4; x++) {
x = r.nextInt(70);
String xx = Integer.toString(x);
R3.setText(xx);
}
for (int c = num3; c == num5; c++) {
c = r.nextInt(70);
String cc = Integer.toString(c);
R3.setText(cc);
}
for (int b = num4; b == num5; b++) {
b = r.nextInt(70);
String bb = Integer.toString(b);
R4.setText(bb);
}
} catch (Exception e){
Toast.makeText(Main_Activity.this,e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Does anyone know why? If so, your help would be deeply appreciated! Thanks.
The possible reason what I can figure out reading your question is most probably you would have forgot the add the button in your another layout.
Since, the Button View would be null in this case so once you click, it would throw NullPointerException like this
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setEnabled(boolean)' on a null object reference
First of all check in your another layout whether the button view is added or not.
Then in Activity class check whether button view that you have initialized using the correct id which should match against the xml one.
These could be the reason for the crash you encountered.
Hope this helps.
Add a try-catch block in button click like..
try{
//Your Code for randomize number.
}
catch(Exception e)
{
Toast.makeText(context,e.getMessage(), Toast.LENGTH_SHORT).show();
}
This will show exception in toast and your app won't crash.
Writing a program in Android Studio 2.2.2 (MainActivity.java). Basically there are multiple inputs, one button, multiple outputs. However, many of the outputs are cross referenced into many of the other outputs. I'm new to all of this as our professor threw this on us in a non-programming class. Can anyone see some of my errors? I hope that I'm doing the process mostly correct, but the operations are all have issues with string/float/double incompatibilities. Thanks for any insight!
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnCalc;
private TextView tvaResult;
private TextView tvcResult;
private TextView tvetResult;
private TextView tvphiResult;
private TextView tvMnResult;
private TextView tvphiMnResult;
private TextView tvbeta1Result;
private EditText etB,etD,etH,etAs,etFc,etFy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btnCalc = (Button)findViewById(R.id.btnCalc);
etB = (EditText)findViewById(R.id.etB);
etD = (EditText)findViewById(R.id.etD);
etH = (EditText)findViewById(R.id.etH);
etAs = (EditText)findViewById(R.id.etAs);
etFc = (EditText)findViewById(R.id.etFc);
etFy = (EditText)findViewById(R.id.etFy);
tvaResult = (TextView)findViewById(R.id.tvaResult);
tvcResult = (TextView)findViewById(R.id.tvcResult);
tvetResult = (TextView)findViewById(R.id.tvetResult);
tvphiResult = (TextView)findViewById(R.id.tvphiResult);
tvMnResult = (TextView)findViewById(R.id.tvMnResult);
tvphiMnResult = (TextView)findViewById(R.id.tvphiMnResult);
tvbeta1Result = (TextView)findViewById(R.id.tvbeta1Result);
btnCalc.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Float B = Float.parseFloat(etB.getText().toString());
Float D = Float.parseFloat(etD.getText().toString());
Float H = Float.parseFloat(etH.getText().toString());
Float As = Float.parseFloat(etAs.getText().toString());
Float Fc = Float.parseFloat(etFc.getText().toString());
Float Fy = Float.parseFloat(etFy.getText().toString());
Float aResult = Float.parseFloat(tvaResult.getText().toString());
Float cResult = Float.parseFloat(tvcResult.getText().toString());
Float etResult = Float.parseFloat(tvetResult.getText().toString());
Float beta1Result = Float.parseFloat(tvbeta1Result.getText().toString());
Float phiResult = Float.parseFloat(tvphiResult.getText().toString());
switch(view.getId() ) {
case R.id.btnCalc:
tvaResult = (Fy * As) / (0.85 * Fc * B);
tvcResult = aResult / beta1Result;
tvetResult = ((D - cResult) / (cResult)) * 0.003;
if (Fc <= 4000) {
beta1Result = 0.85;
} else if (4000 < Fc <= 8000) {
beta1Result= ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
} else {
beta1Result= 0.65;
}
if (etResult >= 0.005) {
tvphiResult= 0.9;
} else if (0.002 <= etResult < 0.005) {
tvphiResult= 0.65 + ((etResult - 0.002) * ((0.25) / (0.005 - 0.002)));
} else {
tvphiResult= 0.00
}
tvMnResult= (Fy * As) * (etD - (aResult / 2));
tvphiMnResult= phiResult * tvMnResult
}}
}}
To make it short, you need to keep the type of each Variable.
Here for example :
tvaResult = (Fy * As) / (0.85 * Fc * B);
You have
TextView = (Float * Float) / ( Double * Float * Float)
TextView = Double // Not possible
You are trying to put a Float value into a Instance of TextView, this can't be done of course. But you really want to update the text of the Textview, so use the methods setText(String) of TextView like :
tvaResult.setText((Fy * As) / (0.85 * Fc * B) + ""); // Need to be convert into String
To change the text to print
This is basicly always the same mistake done, you need to change the text of the TextView an not trying to change the instance itself.
The app crash when there is no text !
I don't have what 'if' to put to fix that !
Thanks you
enter TextView TotalTextView;
EditText EnterPercentage;
EditText EnterPrice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TotalTextView = (TextView) findViewById(R.id.TotalTextView);
EnterPercentage = (EditText) findViewById(R.id.EnterPercentage);
EnterPrice = (EditText) findViewById(R.id.EnterPrice);
Button CalcBtn = (Button) findViewById(R.id.CalcBtn);
CalcBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
float percentage = Float.parseFloat(EnterPercentage.getText().toString());
float prix = Float.parseFloat(EnterPrice.getText().toString());
float dec = percentage / 100;
float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
TotalTextView.setText(String.format("%.2f", total));
}
});
Thank for your answer, I am a begineer so... Thanks !
if (EnterPercentage.getText().equals("")) {
float percentage = Float.parseFloat(EnterPercentage.getText().toString());
float prix = Float.parseFloat(EnterPrice.getText().toString());
float dec = percentage / 100;
float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
TotalTextView.setText(String.format("%.2f", total));
} else {
}
the button don't do anything but the app doesn't crash
We check the length of the String... when it's 0 then we do nothing. When the String > 0 your code is running.
Like:
if (EnterPercentage.getText().trim().toString().length() == 0 || EnterPrice.getText().toString().length() == 0 ) {
//Textfields are empty.
Log.d("Error","Fields are empty");
} else {
//Textfield is full
float percentage = Float.parseFloat(EnterPercentage.getText().toString());
float prix = Float.parseFloat(EnterPrice.getText().toString());
float dec = percentage / 100;
float total = prix - dec * Float.parseFloat(EnterPrice.getText().toString());
TotalTextView.setText(String.format("%.2f", total));
}
You need to check (obviously) your string. The if that your code is missing is :
if (TextUtils.isEmpty(EnterPercentage.getText()) || TextUtils.isEmpty(EnterPrice.getText())) {
// YOUR CODE
}
Good luck with android development.
Questions was already posted
You surely got a NullPointerException. That's because the text is null and you put a .toString() after.
Try this:
float percenteage = 0;
float prix = 0;
if (EnterPercentage.getText() != null)
percenteage = Float.parseFloat(EnterPercentage.getText().toString());
if (EnterPrice.getText() != null)
prix = Float.parseFloat(EnterPercentage.getText().toString());
you need to validate before trying to convert to float
float percentage = getFloat();
private float getFloat(){
float fRet = 0.0f;
if(your validation here){
fRet = Float.parseFloat(EnterPercentage.getText().toString());;
}
return fRet;
}