Validation for Alert dialog

Posted by Hossam on Stack Overflow See other posts from Stack Overflow or by Hossam
Published on 2012-12-18T10:31:34Z Indexed on 2012/12/18 11:03 UTC
Read the original article Hit count: 345

Hi Everyone, Im working in a game and when the player finished the level , Im asking the Player to enter his name on EditText in Alert Dialog :

AlertDialog.Builder builder = new Builder(this);
        LayoutInflater inflater = getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout

        builder.setView(inflater.inflate(R.layout.dialog_name, null))
        // Add action buttons
               .setPositiveButton(R.string.alertName, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {

                       playerText = (EditText) ((AlertDialog)dialog).findViewById(R.id.username);
                       if(playerText.getText() != null && playerText.getText().length() != 0)
                           Helper.currentPlayer.setName(playerText.getText().toString());
                       //calculate the score

                       // save in data base
                       PlayerDataSource playerDS = new PlayerDataSource(getApplicationContext());
                       playerDS.open();
                       Helper.currentPlayer = playerDS.createPlayer(Helper.currentPlayer.getName(), Helper.currentPlayer.getScore(),
                               Helper.currentPlayer.levels, Helper.currentPlayer.times, LanguageHelper.div);
                       playerDS.close();
                       saveStmc();
                       mainScene.setChildScene(choicesMenu, false, true, true);

                   }
               })
               .setTitle("Please Enter Your name")
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               });
        alert = builder.create();

When the Player enters his name and press OK (Positive button) I will check in database if the name exists or not, if the name is exist : show Toast and not dismiss the alert to write another name. How to do that?

© Stack Overflow or respective owner

Related posts about android

Related posts about query