-2

Hi Im trying to learn Java for android and I can't get the simplest code to work. I'm trying to write a percent calc. The code should work, but it wont let me convert float to string.

totalTextView = (TextView) findViewById(R.id.totalTextView);
    percentageTxt = (EditText) findViewById(R.id.percentagetxt);
    numberTxt = (EditText) findViewById(R.id.numbertext) ;

Button calcbutton = (Button) findViewById(R.id.calcbutton);
    calcbutton.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View view){
            float percentage = Float.parseFloat(percentageTxt.getText().toString());
            float dec = percentage / 100;
            float total = dec * Float.parseFloat(numberTxt.getText().toString());
        totalTextView.setText(Float.toString(total));
    }
    });

    }

When i try to do it seperatly with an extra variable there are no errors, but the program still tells me 10 % of 100 is 1 XD.

totalTextView = (TextView) findViewById(R.id.totalTextView);
    percentageTxt = (EditText) findViewById(R.id.percentagetxt);
    numberTxt = (EditText) findViewById(R.id.numbertext) ;

Button calcbutton = (Button) findViewById(R.id.calcbutton);
    calcbutton.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View view){
            float percentage = Float.parseFloat(percentageTxt.getText().toString());
            float dec = percentage / 100;
            float total = dec * Float.parseFloat(numberTxt.getText().toString());
        final String s = Float.toString(total);
                totalTextView.setText(s);
    }
    });

    }

also i'm having trouble formatting the elements. drag and drop doesn't work. They all just get stacked. If I enter translation value individualy, i can't see half the elements in my content main.

!https://i.stack.imgur.com/WnbEe.jpg

I know this is beginner stuff, that's probably why I cant find any other posts on this, ;) but i just can't figure it out.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

2

Try String.valueOf(floarValue) instead of Float.toString(floatValue)

Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20
  • thank you, that worked, I also noticed that the TextView I had was only for one didgit for some reason. but still wierd that `Float.toString(float)` doesn't work – Patrick Ernst May 23 '17 at 22:25