-1

I want to change the content of the edittext megapixel to be the product of the width and height text boxes to make a megapixel calculator.

public void calculate()
{

    EditText tb_widthpx = (EditText) findViewById(R.id.txtbox_widthpx);
    EditText tb_heightpx = (EditText)findViewById(R.id.txtbox_heigthpx);
    EditText tb_megapixel = (EditText) findViewById(R.id.txtbox_megapixel);

    if (tb_widthpx.getText().toString() != "" && tb_heightpx.getText().toString() !="")
    {
        int produkt;
        int width = Integer.parseInt(tb_widthpx.getText().toString());
        int height = Integer.parseInt((tb_heightpx.getText().toString()));

        produkt = width * height;
        tb_megapixel.setText(String.valueOf(produkt));
    }
Celeo
  • 5,583
  • 8
  • 39
  • 41
Vortek
  • 13
  • 5

2 Answers2

1

In Java you have to use .equals() so try this,

public void calculate()
{

    EditText tb_widthpx = (EditText) findViewById(R.id.txtbox_widthpx);
    EditText tb_heightpx = (EditText)findViewById(R.id.txtbox_heigthpx);
    EditText tb_megapixel = (EditText) findViewById(R.id.txtbox_megapixel);

    if (!tb_widthpx.getText().toString().equals("") && !tb_heightpx.getText().toString().equals(""))
    {
        int produkt;
        int width = Integer.parseInt(tb_widthpx.getText().toString());
        int height = Integer.parseInt((tb_heightpx.getText().toString()));

        produkt = width * height;
        tb_megapixel.setText(String.valueOf(produkt));
    }

Reference: Why you use .equals()

Community
  • 1
  • 1
Gary Holiday
  • 3,297
  • 3
  • 31
  • 72
0

You can use .equals() to compare Strings. Like this:

if (!tb_widthpx.getText().toString().equals("") && !tb_heightpx.getText().toString().equals("")) {
    int produkt;
    int width = Integer.parseInt(tb_widthpx.getText().toString());
    int height = Integer.parseInt((tb_heightpx.getText().toString()));

    produkt = width * height;
    tb_megapixel.setText(String.valueOf(produkt));
}

But for a better performance you should use .length(). Like this:

if (!tb_widthpx.getText().toString().length() == 0 && !tb_heightpx.getText().toString().length() == 0) {
    int produkt;
    int width = Integer.parseInt(tb_widthpx.getText().toString());
    int height = Integer.parseInt((tb_heightpx.getText().toString()));

    produkt = width * height;
    tb_megapixel.setText(String.valueOf(produkt));
}
mpostal
  • 355
  • 2
  • 8
  • 19