Android app - Multiply and divide the user entered values values in the two forms

29,730

Solution 1

For taking inputs take 3 EditText and take one Button for get Result by Clicking on it.

Follow this

public class result extends Activity
{
 private EditText edit1;
private EditText edit2;
private EditText edit3;

public void onCreate(Bundle savedInstanceState) 
{
    try
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

        edit1 = (EditText)findViewById(R.id.edit1);
        edit2 = (EditText)findViewById(R.id.edit2);
        edit3 = (EditText)findViewById(R.id.edit3);

        Button click = (Button)findViewById(R.id.btn);

        click.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                int a = Integer.parseInt(edit1.getText().toString());
                int b = Integer.parseInt(edit2.getText().toString());
                int c = Integer.parseInt(edit3.getText().toString());
                double result = ((double) a/b)*c;
                Toast.makeText(result.this, Double.toString(result),Toast.LENGTH_LONG).show();
            }
        });


    }catch (Exception e) {
        e.printStackTrace();
    }
}
}

Result.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#fff"
 >

<EditText 
   android:id="@+id/edit1"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:inputType="text"/>

<EditText 
   android:id="@+id/edit2"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:inputType="text"/>

 <EditText 
   android:id="@+id/edit3"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:inputType="text"/>

 <Button
   android:id="@+id/btn"
   android:layout_width="fill_parent"
   android:layout_height="40dp"
   android:text="Click"/>

</LinearLayout>

Solution 2

Declare 3 EditTexts in your applications layout, as well as a button, and a TextView. Give them unique id's.

The following code will do what you want. It's very simple, so make sure you don't just copy and paste,and that you understand it. I always find it easier to learn from examples, which is why I'm giving one. I hope it helps.

public class MainActivity extends Activity {
    //Declare textviews as fields, so they can be accessed throughout the activity.
EditText mEditText1;
EditText mEditText2;
EditText mEditText3;
TextView mTextView;
Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Bind the EditText views

    mEditText1 = (EditText)findViewById(R.id.editText1);
    mEditText2 = (EditText)findViewById(R.id.editText2);
    mEditText3 = (EditText)findViewById(R.id.editText3);
    mTextView = (TextView)findViewById(R.id.textView1);

    mButton =  (Button)findViewById(R.id.calculateButton);
    mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //When the button is clicked, call the calucate method.
            calculate();
        }
    });

}

public void calculate(){
    //get entered texts from the edittexts,and convert to integers.
    Double value1 = Double.parseDouble(mEditText1.getText().toString());
    Double value2 = Double.parseDouble(mEditText2.getText().toString());
    Double value3 = Double.parseDouble(mEditText3.getText().toString());
    //do the calculation
    Double calculatedValue = (value2/value1)*value3;
    //set the value to the textview, to display on screen.
    mTextView.setText(calculatedValue.toString());
}

}

Share:
29,730
Joshua F
Author by

Joshua F

Updated on January 23, 2022

Comments

  • Joshua F
    Joshua F over 2 years

    I'm trying to make an android app, and as I am a total beginner, I am wondering if anyone could help me out with the code.

    There will be three boxes to enter different numbers, and I want the app to divide the second value by the first, and then multiply it by the third. And then display the answer on screen.

    And the app does have a purpose aha.

    So like (b/a)*c

    • Stanley Mungai
      Stanley Mungai over 11 years
      There is no Helping with code like that.Please Let us see what you have Done and we'll Help
    • Joshua F
      Joshua F over 11 years
      Wow! Thanks so much for your fast replies. Okay, I'm sorry if I have no idea what I'm doing aha. My friend is making it and I am trying to find bits of code for him. There's a lot of code, but I think this is the bit you are asking for?: It will have to be in two parts because the message will run out of characters.
    • Joshua F
      Joshua F over 11 years
      public class MainActivity extends Activity implements View.OnClickListener{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void OnClick(View v){} public void helpbtn(View v){ new AlertDialog.Builder(this) .setTitle("Help") .setMessage("Numbers" + "Value A" + "Value B" + "Value C")
    • Joshua F
      Joshua F over 11 years
      .setNeutralButton("I get it now", null) .show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClick(View v) { // TODO Auto-generated method stub } }
    • Joshua F
      Joshua F over 11 years
      Okay it didn't keep like the lines and stuff, so I posted a new question so you could see it properly: stackoverflow.com/questions/12932336/…