Android app - Multiply and divide the user entered values values in the two forms
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>
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());
}
}