1

My layout is:

1 EditText (id = etvalue1)
1 EditText (id = etvalue2)
1 Button (id = save)

I need to know how to do the following with these widgets:

  • If the User clicks on the button with the EditText empty display a message and wait for the User to enter the value.
  • Display the message until the User and enter the value after the value is to enter the calculation (etvalue1 + etvalue2) and show the result.
Orkun
  • 6,998
  • 8
  • 56
  • 103
GDawson
  • 337
  • 1
  • 7
  • 19

6 Answers6

3

In Activity Class write this code in onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText1 = (EditText)findViewById(R.id.etvalue1);
    editText2 = (EditText)findViewById(R.id.etvalue2);
    button = (Button)findViewById(R.id.save);

    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if(!(editText1.getText().toString().equalsIgnoreCase(""))){
                if(!(editText2.getText().toString()).equalsIgnoreCase("")){
                    addNumbers();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Enter no.", Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(getApplicationContext(),
                        "Enter no.", Toast.LENGTH_LONG).show();
            }

        }

        private void addNumbers() {

        }
    });

}

}

In addNumbers method get the value from EditTest and do whatever you want to do.

Vinit ...
  • 1,409
  • 10
  • 37
  • 66
2

In onclicklistener for the button do the following

if(edit1.getText().equals("") || edit2.getText().equals(""))  
{
     //toast the error message 
     return;
}
 //calculate the result

The maximum time for toast is Toast.LENGTH_LONG. If you want the message to be displayed till the user enters data in edittext, then use a textview to display the message. Set onTextChangedListener on both the EditTexts to check if the text is changed. in afterTextChanged method you can do the calculation.

user936414
  • 7,574
  • 3
  • 30
  • 29
1
if(edit1.getText().equals("") || edit2.getText().equals(""))  
{
     //toast the error message 
     Dialog dialog=new Dialog(getApplicationContext());
     LinearLayout llView=new LinearLayout(getApplicationContext());
     EditText editText1=new EditText(getApplicationContext());
     EditText editText2=new EditText(getApplicationContext());
     wsutText2.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            //do your calculations
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    }); 

    editText2.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            //do your calculations
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    }); 
llView.addView(editText1);
llView.addView(editText2);
dialog.setContentView(llView);
dialog.show();
     return;
}
jeet
  • 29,001
  • 6
  • 52
  • 53
0

If you want to detect when the user enters values in the edittext, you need to use events to observe the changes as in Counting Chars in EditText Changed Listener

Read the relevant methods in the reference

Alternatively, you can just make that check on the click event of your button:

button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 if(etvalue1.getText().toString().equals(''))
                     //empty text condition
             }
         });
Community
  • 1
  • 1
Orkun
  • 6,998
  • 8
  • 56
  • 103
0

You can use below code to determine if the EditText is empty:

String sEdit1 = Edit1.getText().toString();

if (sEdit1.matches(""))
        {
            Toast.makeText(getBaseContext(), "EditText is empty", Toast.LENGTH_SHORT).show();     
            return;
        }
Lars
  • 915
  • 4
  • 18
  • 27
0
if(edit1.getText().equals("") || edit1.getText().equals(null))  {
    //toast the message 
} else {
    //calculate the result
}
Yury
  • 20,618
  • 7
  • 58
  • 86
Maulik J
  • 2,745
  • 19
  • 22