<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1"
android:background="#0f0" >
<EditText
android:layout_width="336dp"
android:layout_height="wrap_content"
android:id="@+id/tfNum1" />
<EditText
android:layout_width="334dp"
android:layout_height="wrap_content"
android:id="@+id/tfNum2" />
<TextView
android:layout_width="333dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/emptystring"
android:id="@+id/Lresult" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.05">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/Badd"
android:layout_weight="0.05"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" -"
android:id="@+id/Bsub"
android:layout_weight="0.05"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/Bmul"
android:layout_weight="0.05"
android:onClick="onButtonClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/Bdiv"
android:layout_weight="0.05"
android:onClick="onButtonClick" />
</LinearLayout>
</LinearLayout>
The Java file below is named as MainActivity.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Double num1,num2,ans;
Boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v)
{
EditText a1=(EditText) findViewById(R.id.tfNum1);
EditText a2=(EditText) findViewById(R.id.tfNum2);
TextView tv=(TextView) findViewById(R.id.Lresult);
num1=Double.parseDouble(a1.getText().toString());
num2=Double.parseDouble(a1.getText().toString());
if (v.getId()==R.id.Badd)
ans=num1+num2;
if (v.getId()==R.id.Bsub)
ans=num1-num2;
if (v.getId()==R.id.Bmul)
ans=num1*num2;
if(v.getId()==R.id.Bdiv)
if(num2==0)
flag=true;
else
ans=num1/num2;
tv.setText(ans+"");
}
}
No comments:
Post a Comment