Reputation: 429
I have an if condition which checks if the edittext is empty or not,if it is empty an alert is raised. But when I enter some values to the edittext then also it is giving me the alert
package com.example.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calci extends Activity {
TextView t1;
EditText e1, e2;
Button add, sub, mul, div;
Context c=this;
String b, a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
e1 = (EditText) findViewById(R.id.EditText01);
e2 = (EditText) findViewById(R.id.EditText02);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
t1 = (TextView) findViewById(R.id.textView1);
a = e1.getText().toString();
b = e2.getText().toString();
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (a.equals("") || b.equals("")){
AlertDialog.Builder a1 = new AlertDialog.Builder(c);
// Setting Dialog Title
a1.setTitle("Alert Dialog");
// Setting Dialog Message
a1.setMessage("PLEASE ENTER SOMETHING");
a1.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int button1) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// Showing Alert Message
AlertDialog alertDialog = a1.create();
a1.show();
} else {
Log.d("sadasd", "msg");
int result = Integer.parseInt(a) + Integer.parseInt(b);
t1.setText(Integer.toString(result));
// InputMethodManager imm = (InputMethodManager)getSystemService(
// Context.INPUT_METHOD_SERVICE);
// imm.hideSoftInputFromWindow(add.getWindowToken(), 0);
}
}
});
}
}
I have an if condition which checks if the edittext is empty or not,if it is empty an alert is raised.But when I enter some values to the edittext then also it is giving me the alert.
Upvotes: 1
Views: 126
Reputation: 3684
Your condition if (a.equals("") || b.equals(""))
will not be passing only when both a
and b
will have some values inside. So simply both editTexts
should be filled in and you should check it also on the top of onClick listener because at that moment you simply do it only when your activity starts. So you have old values in a and b
variables.
add.setOnClickListener(new View.OnClickListener() {
a = e1.getText().toString();
b = e2.getText().toString();
Upvotes: 0
Reputation: 29632
You are doing it wrong way, by doing it in onCreate()
method. You should do it in onClick()
method like below,
@Override
public void onClick(View arg0)
{
a = e1.getText().toString();
b = e2.getText().toString();
if (a.equals("") || b.equals(""))
{
The actual reason is that in OnCreate() method EditText is blank as you have just initilize it. What you need is to insert a text init and then check for the condition. hence you need to write a = e1.getText().toString();
& b = e2.getText().toString();
statement in onClicck() method
Upvotes: 1
Reputation: 941
This must be inside on click listner to get updated value before the button was pressed :
a = e1.getText().toString();
b = e2.getText().toString();
Upvotes: 0
Reputation: 24853
Try this..
Get the EditText
text inside the ClickListener
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
a = e1.getText().toString();
b = e2.getText().toString();
if (a.equals("") || b.equals("")){
.
.
.
}
Upvotes: 1