package infoocode.blogspot.com.sonycalculator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, View.OnClickListener {
Spinner sp03;
TextView no1, no2, resulttxtvew;
Button breset;
Double rslt2, mynum1, mynum2;
///////// Insert Values for Spinner in Array /////////
String[] sp1 = {"NULL", "+", "-", "*", "/"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
no1 = (TextView) findViewById(R.id.nmbr1txtvew);
no2 = (TextView) findViewById(R.id.nmbr2txtvew);
resulttxtvew = (TextView) findViewById(R.id.rslttxtvew);
sp03 = (Spinner) findViewById(R.id.sp3);
breset = (Button) findViewById(R.id.resetbutton);
breset.setOnClickListener(this);
------------------------------------------------------------------------------------------------------------------------
///////// Insert Values in the Spinner through ArrayAdapter /////////
ArrayAdapter<String> adapter = new <String>ArrayAdapter(this, android.R.layout.simple_spinner_item, sp1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp03.setAdapter(adapter);
sp03.setOnItemSelectedListener(this);
Toast.makeText(this, "Welcome to Spinner Calculator", Toast.LENGTH_SHORT).show();
}
@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.
return super.onOptionsItemSelected(item);
}
-----------------------------------------------------------------------------------------------------------------
///////// Override methods of ArrayAdapter for Spinner /////////
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
///////////////// If the TextViews is Not Empty ///////////////////////////
if (no1.getText().length() > 0 && no2.getText().length() > 0) {
if (sp03.getSelectedItem() == "+") {
Toast.makeText(parent.getContext(), "Your Selected Operator is " + sp03.getSelectedItem(), Toast.LENGTH_SHORT).show();
mynum1 = Double.parseDouble(no1.getText().toString());
mynum2 = Double.parseDouble(no2.getText().toString());
rslt2 = mynum1 + mynum2;
resulttxtvew.setText("Your Result Is : " + rslt2);
breset.setVisibility(View.VISIBLE);
} else if (sp03.getSelectedItem() == "-") {
Toast.makeText(parent.getContext(), "Your Selected Operator is " + sp03.getSelectedItem(), Toast.LENGTH_SHORT).show();
mynum1 = Double.parseDouble(no1.getText().toString());
mynum2 = Double.parseDouble(no2.getText().toString());
rslt2 = mynum1 - mynum2;
resulttxtvew.setText("Your Result Is : " + rslt2);
breset.setVisibility(View.VISIBLE);
} else if (sp03.getSelectedItem() == "*") {
Toast.makeText(parent.getContext(), "Your Selected Operator is " + sp03.getSelectedItem(), Toast.LENGTH_SHORT).show();
mynum1 = Double.parseDouble(no1.getText().toString());
mynum2 = Double.parseDouble(no2.getText().toString());
rslt2 = mynum1 * mynum2;
resulttxtvew.setText("Your Result Is : " + rslt2);
breset.setVisibility(View.VISIBLE);
} else if (sp03.getSelectedItem() == "/") {
Toast.makeText(parent.getContext(), "Your Selected Operator is " + sp03.getSelectedItem(), Toast.LENGTH_SHORT).show();
mynum1 = Double.parseDouble(no1.getText().toString());
mynum2 = Double.parseDouble(no2.getText().toString());
rslt2 = mynum1 / mynum2;
resulttxtvew.setText("Your Result Is : " + Double.toString(rslt2));
breset.setVisibility(View.VISIBLE);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
//If the TextViews is Empty
else if(no1.getText().length() < 1 && no2.getText().length() < 1) {
if (sp03.getSelectedItem() == "+" || sp03.getSelectedItem() == "-"
|| sp03.getSelectedItem() == "*" || sp03.getSelectedItem() == "/") {
Toast.makeText(this, "Please, Enter the Numbers to Perform an Action.\n\t\t\t\t\t\t\t\t\t\tThanks !", Toast.LENGTH_SHORT).show();
sp03.setSelection(0);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
////////////////// Method to Perform action on reset Button //////////////////////////
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.resetbutton) {
Toast.makeText(this, "Your Calculator is Reset", Toast.LENGTH_SHORT).show();
no1.setText("");
no2.setText("");
resulttxtvew.setText("WELCOME BACK\n\tYour Result is HERE\nAfter Selecting Operator...");
resulttxtvew.setTextColor(Color.BLUE);
sp03.setSelection(0);
breset.setVisibility(View.INVISIBLE);
}
}
}
No comments:
Post a Comment