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?
See this:
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();
/////Here check for name in database
if (name does not exist)
{
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);
builder.dismiss()
}
else
{
Toast.makeText(getApplicationContext(), "Enter other name..",
Toast.LENGTH_SHORT).show();
}
}
})
.setTitle("Please Enter Your name")
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setCanceledOnTouchOutside(false);
alert = builder.create();
builder.show()
I have found the solution, it is based on overriding the OnClick()
for positive button of the AlertDialog
and here is the solution:
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))
.setPositiveButton(R.string.alertName, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setTitle("Please Enter Your name");
builder = builder.setCancelable(false);
alert = builder.create();
alert.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
alert = (AlertDialog)dialog;
Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playerText = (EditText) ((AlertDialog)alert).findViewById(R.id.username);
if(playerText.getText() == null || playerText.getText().length() == 0)
{
showToastMsg("Please enter valid name");
}
else
{
if(nameIsExist())
{
showToastMsg("Name already exist!!");
}
else
{
//Do stuff because every this is OK
alert.dismiss();
}
}
}
});
}
});
Set the dilaog as cancelable true and overide the onBackPressed() Method if need and
AlertDialog.Builder builder = new Builder(this);
builder.setCancelable(true);
// onBackPressed set it to true when you meet you requisite in Dialog box
@Override
public void onBackPressed(){
if(IsNameInserted)
super.onBackPressed();
}