Create simple application for Add or Subtract two numbers using android studio.
User Interface
Layout File
activity_add.xml
Here is the XML code for the design.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.root.layouts.AddActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fadcdc"
android:layout_centerVertical="true"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Number 1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000"
android:textSize="20sp"
/>
<EditText
android:id="@+id/edtNumber1"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Number 2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000"
android:textSize="20sp"
/>
<EditText
android:id="@+id/edtNumber2"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<Button
android:id="@+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textColor="#000"
/>
<Button
android:id="@+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textColor="#000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Result"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000"
android:textSize="20sp"
/>
<EditText
android:id="@+id/edtResult"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20sp"
android:focusable="false"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Activity java file
AddActivity.java
package com.example.root.layouts;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends AppCompatActivity {
EditText edtNumber1;
EditText edtNumber2;
EditText edtResult;
Double num1,num2,result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
edtNumber1 = (EditText)findViewById(R.id.edtNumber1);
edtNumber2 = (EditText)findViewById(R.id.edtNumber2);
edtResult = (EditText)findViewById(R.id.edtResult);
Button btnPlus = (Button)findViewById(R.id.btnPlus);
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1=Double.parseDouble(edtNumber1.getText().toString());
num2=Double.parseDouble(edtNumber2.getText().toString());
result = num1+num2;
edtResult.setText(Double.toString(result));
}
});
Button btnMinus = (Button)findViewById(R.id.btnMinus);
btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1=Double.parseDouble(edtNumber1.getText().toString());
num2=Double.parseDouble(edtNumber2.getText().toString());
result = num1-num2;
edtResult.setText(Double.toString(result));
}
});
}
}
Video for this tutorials
No comments:
Post a Comment