,Start a new project and name it. To design the layout, drag and drop the
components into the emulator. For a calculator, we need two ‘EditText’
where the user should provide the inputs for calculation and four buttons
for the operations. The TextView is used to display the results of the
operations.
Now, go to the Text file of the ‘activity_main.xml’, you will see that the
design tags are implicitly added.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_first_number"
android:hint="Enter first number"
android:inputType="number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_second_number"
android:layout_below="@+id/editText_first_number"
android:layout_centerHorizontal="true"
android:inputType="number"
android:hint="Enter Second Number"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result"
android:textSize="25sp"
android:id="@+id/textView_result"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/button_add"
android:layout_below="@+id/textView_result"
android:layout_alignParentLeft="true"
android:layout_marginTop="48dp"
android:layout_toLeftOf="@+id/textView_result"
android:layout_toStartOf="@+id/textView_result" />
<Button
android:layout_width="wrap_content",
android:layout_height="wrap_content"
android:text="Subtract"
android:id="@+id/button_sub"
android:layout_alignTop="@+id/button_add"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textView_result"
android:layout_toEndOf="@+id/textView_result" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:id="@+id/button_mul"
android:layout_alignTop="@+id/button_div"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button_add"
android:layout_alignEnd="@+id/button_add" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:id="@+id/button_div"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true",
android:layout_alignParentEnd="true"
android:layout_marginBottom="40dp"
android:layout_alignLeft="@+id/button_sub"
android:layout_alignStart="@+id/button_sub" />
The xml tags added for EditText, Button and TextView are the default
versions.
You can change the outlook and behaviour of the user interface
according to the preference or stick to the default ones. However, it’s a
good practice to rename the Id as it adds hints for user’s reference.
Now go to the MainActivity.java file to build the logic for the calculator.
,
public class MainActivity extends Activity implements
View.OnClickListener {
EditText editText_first_number;
EditText editText_second_number;
Button button_add;
Button button_sub;,
Button button_mul;
Button button_div;
TextView textView_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_first_number=(EditText)
findViewById(R.id.editText_first_number);
editText_second_number=(EditText)
findViewById(R.id.editText_second_number);
button_add= (Button) findViewById(R.id.button_add);
button_add.setOnClickListener(this);
button_sub=(Button) findViewById(R.id.button_sub);
button_sub.setOnClickListener(this);
button_mul=(Button) findViewById(R.id.button_mul);
button_mul.setOnClickListener(this);
button_div=(Button) findViewById(R.id.button_div);,
button_div.setOnClickListener(this);
textView_result= (TextView) findViewById(R.id.textView_result);
}
@Override
public void onClick(View v) {
int num1=Integer.parseInt(editText_first_number.getText().toString());
int
num2=Integer.parseInt(editText_second_number.getText().toString());
int result=0;
switch (v.getId()){
case R.id.button_add:
result=num1+num2;
break;
case R.id.button_sub:
result=Math.abs(num1-num2);
break;,
case R.id.button_mul:
result=Math.abs(num1*num2);
break;
case R.id.button_div:
if(num2!=0){
result=Math.abs(num1/num2);
}else
{
Toast.makeText(getBaseContext(),"Second number Should
not be zero" , Toast.LENGTH_LONG).show();
}
Break;
}
textView_result.setText("Result "+result);
}
}
Declare the variables for the EditText, Button and TextView preferably with
the Id provided in the Text file. Now, fetch the Id from the user interface.
The retrieved value is an integer hence it is type casted before assigning to
the variables.
Now declare two variables with the integer datatypes and define it with
parsed value fetched from the EditText with the help of getText() function.
To build the logic for the operation get the Id of the button and implement
it in the switch option.
So when the user clicks on the Add button, the button id is passed and the
switch case for add is executed.
The same logic is implemented on each ,
switch cases for all the operations.
The Math.abs () gives the absolute
values for the operation.
All this is done with the onClick() event block.
The else block will be executed when the input rules are violated.
The
Toast.makeText () function has three parameters, the state of the
application, the message to be popped and duration or which it needs to be
popped.
In this application when the users enters zero as second number,
the else block will be executed notifying the user to give correct inputs.,,
components into the emulator. For a calculator, we need two ‘EditText’
where the user should provide the inputs for calculation and four buttons
for the operations. The TextView is used to display the results of the
operations.
Now, go to the Text file of the ‘activity_main.xml’, you will see that the
design tags are implicitly added.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_first_number"
android:hint="Enter first number"
android:inputType="number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_second_number"
android:layout_below="@+id/editText_first_number"
android:layout_centerHorizontal="true"
android:inputType="number"
android:hint="Enter Second Number"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result"
android:textSize="25sp"
android:id="@+id/textView_result"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/button_add"
android:layout_below="@+id/textView_result"
android:layout_alignParentLeft="true"
android:layout_marginTop="48dp"
android:layout_toLeftOf="@+id/textView_result"
android:layout_toStartOf="@+id/textView_result" />
<Button
android:layout_width="wrap_content",
android:layout_height="wrap_content"
android:text="Subtract"
android:id="@+id/button_sub"
android:layout_alignTop="@+id/button_add"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textView_result"
android:layout_toEndOf="@+id/textView_result" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:id="@+id/button_mul"
android:layout_alignTop="@+id/button_div"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button_add"
android:layout_alignEnd="@+id/button_add" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:id="@+id/button_div"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true",
android:layout_alignParentEnd="true"
android:layout_marginBottom="40dp"
android:layout_alignLeft="@+id/button_sub"
android:layout_alignStart="@+id/button_sub" />
The xml tags added for EditText, Button and TextView are the default
versions.
You can change the outlook and behaviour of the user interface
according to the preference or stick to the default ones. However, it’s a
good practice to rename the Id as it adds hints for user’s reference.
Now go to the MainActivity.java file to build the logic for the calculator.
,
public class MainActivity extends Activity implements
View.OnClickListener {
EditText editText_first_number;
EditText editText_second_number;
Button button_add;
Button button_sub;,
Button button_mul;
Button button_div;
TextView textView_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_first_number=(EditText)
findViewById(R.id.editText_first_number);
editText_second_number=(EditText)
findViewById(R.id.editText_second_number);
button_add= (Button) findViewById(R.id.button_add);
button_add.setOnClickListener(this);
button_sub=(Button) findViewById(R.id.button_sub);
button_sub.setOnClickListener(this);
button_mul=(Button) findViewById(R.id.button_mul);
button_mul.setOnClickListener(this);
button_div=(Button) findViewById(R.id.button_div);,
button_div.setOnClickListener(this);
textView_result= (TextView) findViewById(R.id.textView_result);
}
@Override
public void onClick(View v) {
int num1=Integer.parseInt(editText_first_number.getText().toString());
int
num2=Integer.parseInt(editText_second_number.getText().toString());
int result=0;
switch (v.getId()){
case R.id.button_add:
result=num1+num2;
break;
case R.id.button_sub:
result=Math.abs(num1-num2);
break;,
case R.id.button_mul:
result=Math.abs(num1*num2);
break;
case R.id.button_div:
if(num2!=0){
result=Math.abs(num1/num2);
}else
{
Toast.makeText(getBaseContext(),"Second number Should
not be zero" , Toast.LENGTH_LONG).show();
}
Break;
}
textView_result.setText("Result "+result);
}
}
Declare the variables for the EditText, Button and TextView preferably with
the Id provided in the Text file. Now, fetch the Id from the user interface.
The retrieved value is an integer hence it is type casted before assigning to
the variables.
Now declare two variables with the integer datatypes and define it with
parsed value fetched from the EditText with the help of getText() function.
To build the logic for the operation get the Id of the button and implement
it in the switch option.
So when the user clicks on the Add button, the button id is passed and the
switch case for add is executed.
The same logic is implemented on each ,
switch cases for all the operations.
The Math.abs () gives the absolute
values for the operation.
All this is done with the onClick() event block.
The else block will be executed when the input rules are violated.
The
Toast.makeText () function has three parameters, the state of the
application, the message to be popped and duration or which it needs to be
popped.
In this application when the users enters zero as second number,
the else block will be executed notifying the user to give correct inputs.,,
Last edited by a moderator: