part 7 - Understanding Edit Text Calculate

 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;

public class MainActivity extends AppCompatActivity {

EditText etTwo,
etOne;

TextView tvResult;
Button btnPlus, btnMinus, btnDevide, btnMulitiply;

@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);
btnMinus = (Button)findViewById(R.id.btnMinus);
btnDevide = (Button)findViewById(R.id.btnDevide);
btnMulitiply = (Button)findViewById(R.id.btnMulitiply);


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 = resultAddInteger(c, d);

tvResult.setText("" + e);


}
});


btnMinus.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);


minus(c, d, tvResult);


}
});


btnDevide.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);

float e = (float) c ;
float f = (float) d ;

float p = divide( e, f );


tvResult.setText(""+p );


}
});

btnMulitiply.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);

multiply (c, d, tvResult );


}
});

}

void multiply (int a , int b, TextView txtResult ){

int c = a*b ;

txtResult.setText(""+c );

}


float divide (float n, float m ){

float x = n/m;

return x ;
}



void minus (int a , int b, TextView txtResult ){

int c = a - b;

txtResult.setText(""+c);

}

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 resultAddInteger(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="+"/>


<Button
android:layout_marginTop="25dp"
android:id="@+id/btnMinus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"/>


<Button
android:layout_marginTop="25dp"
android:id="@+id/btnDevide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/"/>


<Button
android:layout_marginTop="25dp"
android:id="@+id/btnMulitiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="x"/>


</LinearLayout>


No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts