0

i am trying to make a simple android application were the user enters a name, height, and weight in three different text fields. once a button is pressed the input should be displayed on text views at the bottom and the height is converted from metres inches and the weight from kilograms to pounds. the problem is if the button is pushed and the is no text in the fields the application crushes!!! how do i solve this by displaying an alert dialog box telling the user to input text before pushing the button. i am new to android so any assistance will be highly appreciated.

Nangu Haci
  • 153
  • 3
  • 5
  • 12
  • 1
    see http://stackoverflow.com/questions/10170313/alert-dialog-validation-in-android or http://stackoverflow.com/questions/13930968/validation-for-alert-dialog – Rachel Gallen Jan 24 '13 at 11:23
  • Please use proper formatting to post your question so you get proper answers. – The iOSDev Jan 24 '13 at 11:47

3 Answers3

2

You can use this method to find whether the user has entered text on Edittext

In the button's Onclick, put the following code

if(name.getText().toString().equals("")) //name is the name of the Edittext in code
            {
                Toast.makeText(getBaseContext(), "You didn't enter the Name",    Toast.LENGTH_SHORT).show();

            }
else if(height.getText().toString().equals("")) //height is the name of the Edittext in code
            {
                Toast.makeText(getBaseContext(), "You didn't enter the Height",    Toast.LENGTH_SHORT).show();

            }
else if(weight.getText().toString().equals("")) //weight is the name of the Edittext in code
            {
                Toast.makeText(getBaseContext(), "You didn't enter the Weight",    Toast.LENGTH_SHORT).show();

            }
else
{
   // put your your code
}
user1954492
  • 495
  • 3
  • 13
1
 String UserName = username_edittext.getText().toString().trim();
    String UserPassword = password_edittext.getText().toString().trim();

    if (!(UserName.equals("") && UserPassword.equals(""))) {        
        Log.i(TAG, "enter uname and pass are empty");
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

            // Setting Dialog Title
            alertDialog.setTitle("Alert");

            // Setting Dialog Message
            alertDialog.setMessage("Enter Value");

            if(status != null)
                // Setting alert dialog icon
                alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

            // Setting OK Button
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                           alertDialog.dissmiss();
                }
            });

            // Showing Alert Message
            alertDialog.show();



    } else {
        Log.i(TAG, "Continue");
    }
0
String UserName = username_edittext.getText().toString().trim();
String UserPassword = password_edittext.getText().toString().trim();

if (!(UserName.equals("") && UserPassword.equals(""))) {        
    Log.i(TAG, "enter uname and pass are empty");
    CreateAlertDialog();     
} else {
    Log.i(TAG, "Continue");
}
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166