edittext.java
===========
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText1, editText2;
private Button addButton, subButton;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1Id);
editText2 = (EditText) findViewById(R.id.editText2Id);
addButton = findViewById(R.id.addButtonId);
subButton = findViewById(R.id.subButtonId);
resultTextView = findViewById(R.id.resultTextViewId);
addButton.setOnClickListener(this);
subButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
String number1 = editText1.getText().toString();
String number2 = editText2.getText().toString();
//converting into double
double num1 = Double.parseDouble(number1);
double num2 = Double.parseDouble(number2);
if (v.getId() == R.id.addButtonId) {
double sum = num1 + num2;
resultTextView.setText("Result " + sum);
}
if (v.getId() == R.id.subButtonId) {
double sub = num1 - num2;
resultTextView.setText("Result " + sub);
}
} catch (Exception e) {
Toast.makeText(MainActivity.this,"Please Enter numbers",Toast.LENGTH_SHORT).show();
}
}
}
===========================================
string.xml
-------------
<resources>
<string name="app_name">EditText</string>
<string name="editText1_text">Enter number 1</string>
<string name="editText2_text">Enter number 2</string>
<string name="add_text">add</string>
<string name="sub_text">sub</string>
</resources>
===========================================
activity_main.xml
-------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
tools:context="com.bdstall.web.edittext.MainActivity">
<EditText
android:id="@+id/editText1Id"
android:layout_width="200dp"
android:layout_height="50dp"
android:textSize="20sp"
android:textStyle="bold"
android:hint="@string/editText1_text"
android:inputType="numberDecimal"
android:backgroundTint="#0e0e0e"
android:textCursorDrawable="@null"
android:layout_marginBottom="15dp"
/>
<EditText
android:id="@+id/editText2Id"
android:layout_width="200dp"
android:layout_height="50dp"
android:textSize="20sp"
android:textStyle="bold"
android:hint="@string/editText2_text"
android:inputType="numberDecimal"
android:backgroundTint="#0e0e0e"
android:textCursorDrawable="@null"
/>
<LinearLayout
android:layout_margin="15dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button
android:text="@string/add_text"
android:id="@+id/addButtonId"
android:textSize="20sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="@string/sub_text"
android:id="@+id/subButtonId"
android:textSize="20sp"
android:layout_margin="1dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<TextView
android:textStyle="bold"
android:id="@+id/resultTextViewId"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
thistimebd Bangladesh Live online newsportal, education, Lifestyle, Health, Photography, gif image etc.
Make your own name or company name website | contact: thistimebd24@gmail.com
Copyright © 2020-2024 News Portal in Bangladesh - THISTIMEBD.COM. ALL Rights Reserved.