Java Part :
package first.learn.edittextproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
EditText etTwo,
etOne;
TextView tvResult;
Button btnPlus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etTwo = (EditText) findViewById(R.id.etTwo);
etOne = (EditText) findViewById(R.id.etOne);
tvResult = (TextView) findViewById(R.id.tvResult);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String a = getTextFromEditText(etOne);
if (a == null) {
return;
}
if (!isIntegerCharacter(a)) {
Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
return;
}
String b = getTextFromEditText(etTwo);
if (b == null) {
return;
}
if (!isIntegerCharacter(b)) {
Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
return;
}
int c = stringToIntger(a);
int d = stringToIntger(b);
int e = resultInteger(c, d);
tvResult.setText("" + e);
}
});
}
boolean isIntegerCharacter(String s) {
int c = -1;
try {
c = Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
int stringToIntger(String s) {
return Integer.parseInt(s);
}
int resultInteger(int a, int b) {
return a + b;
}
String getTextFromEditText(EditText editText) {
String s = editText.getText().toString();
if (TextUtils.isEmpty(s)) {
editText.setError("Empty");
return null;
} else {
editText.setError(null);
return s;
}
}
}
xml part :
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">
<EditText
android:hint="number One...."
android:layout_marginTop="25dp"
android:id="@+id/etOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:hint="number two..."
android:layout_marginTop="25dp"
android:id="@+id/etTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:textSize="25dp"
android:textColor="@color/purple_700"
android:layout_marginTop="20dp"
android:id="@+id/tvResult"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result..."
/>
<Button
android:layout_marginTop="25dp"
android:id="@+id/btnPlus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>
No comments:
Post a Comment