Design a Simple Calculator Application Using Android Studio....(Part 2-Calculator Implementation )
Build a Simple Calculator Performing Arithematic Operations Using Android Studio.....
In The Previous Tutorial We Have Designed The layout Of Calculator...In This Tutorial We Will Be Working On The Implementation Part ie. With The MainActivity.java file
If You are New to the tutorial go through Following Link For Calculator Design Layout
Calculator Layout Design
Lets start...
1.The Layout Of Our Project Looks Something Like This...
Make Sure The Layout Has Four Buttons and Each Button with "onClick" property has been given some value or name.
(Recommended to Provide a Single Name For All Buttons to Avoid Confusion Ahead)...
2.Now Open the "MainActivity.java" file...Which Shall Be Something like this..
3.Create a Method...which should be something like this...
public void <OnClickname>()
{
//Implement Your Logic here...
}
As Shown.....I have Provided the Logic Below...
Content_main.xml is attached In Previous Video You Can Through Above mentioned Link...
MainActivity.java File
4.Hence,Run Your App On Emulator....
Its Working...!!
If You are New or Don't Know How To Run App On Emulator Go to My Following Link...
Run App On Emulator..

Thank You...!!!
In The Previous Tutorial We Have Designed The layout Of Calculator...In This Tutorial We Will Be Working On The Implementation Part ie. With The MainActivity.java file
If You are New to the tutorial go through Following Link For Calculator Design Layout
Calculator Layout Design
Lets start...
1.The Layout Of Our Project Looks Something Like This...
Make Sure The Layout Has Four Buttons and Each Button with "onClick" property has been given some value or name.
(Recommended to Provide a Single Name For All Buttons to Avoid Confusion Ahead)...
2.Now Open the "MainActivity.java" file...Which Shall Be Something like this..
3.Create a Method...which should be something like this...
public void <OnClickname>()
{
//Implement Your Logic here...
}
As Shown.....I have Provided the Logic Below...
Content_main.xml is attached In Previous Video You Can Through Above mentioned Link...
MainActivity.java File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
} | |
}); | |
} | |
public void OnButtonClick(View v) //view object handles button event | |
{ | |
//Create 2 EditTexts | |
EditText a1 = (EditText) findViewById(R.id.Num1text); | |
EditText a2 = (EditText) findViewById(R.id.Num2Text); | |
EditText a3 = new EditText(this); | |
//Create 1 Textview | |
TextView tv = (TextView) findViewById(R.id.Result); | |
//declare the Variables Required | |
double num1,num2,Answer,clear; | |
Answer = 0; | |
//Initilaize Answer to Zero | |
boolean flag; | |
//Flag Required for division by error | |
num1 = Double.parseDouble(a1.getText().toString()); //Convert the Number to String | |
num2 = Double.parseDouble(a2.getText().toString()); | |
if(v.getId() == R.id.plus) | |
Answer = num1 + num2; | |
if(v.getId() == R.id.minus) | |
Answer = num2 - num1; | |
if(v.getId() == R.id.Mul) | |
Answer = num1 * num2; | |
if(v.getId() == R.id.div) | |
if(num2 == 0) | |
flag = true; //if num2 is 0 then Division by 0 error oocurs.. | |
else | |
//else the number is Natural Number... | |
Answer = num1 / num2; | |
//Set the answer with the "result" variable | |
tv.setText("Result Of "+num1+" & "+num2+" is "+Answer+" "); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
4.Hence,Run Your App On Emulator....
Its Working...!!
If You are New or Don't Know How To Run App On Emulator Go to My Following Link...
Run App On Emulator..




Thank You...!!!
Comments
Post a Comment