checking edittext text always return false - java

im trying to get text from edit text and check if it is True or False and if not display an error message
correcttxt=correctans.getText().toString();
if ((!correcttxt.equals("True")) || (!correcttxt.equals("False"))){
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminAdd4.this);
alertBuilder.setTitle("Invalid");
alertBuilder.setMessage("The correct answer should be True or False.");
alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertBuilder.create().show();
}
else {
//confirmation to add exercise
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminAdd4.this);
alertBuilder.setTitle("Add Exercise");
alertBuilder.setMessage("Are you sure you want to add this exercise?");
alertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
new CreateNewexercise().execute();
}
});
alertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertBuilder.create().show();
}
but this will always display the error message even when the text is True or False i don't know why ?
can someone help please ? thank you :)

Related

Alert Dialog with EditText closes if input is empty

I have alert dialog with edit text. User must enter something and if not and then system should alert him that this input is required.
This is code for Alter Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Razlog storniranja?");
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setSingleLine(false);
input.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Otkaži", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
if (m_Text.length()==0)
{
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
}
}
});
builder.show();
But when user click on positive button and input is empty the dialog is closed. Is something missing in my code to avoid closing dialog?
EDIT
I think I found solution. The last line builder.show(); should be deleted and instead, this piece of code should be added:
builder.setView(input);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Boolean wantToCloseDialog = (input.getText().toString().trim().isEmpty());
// if EditText is empty disable closing on possitive button
if (!wantToCloseDialog) {
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
alertDialog.dismiss();
}
}
});
I hope this will help others.
Well, I do not have any clue of what you are trying to do.
The error that I had when using your code was:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
m_Text needs to be initialized.
in case m_Text is not initialized, on the onDismiss method you might need to change:
if (m_Text != null && m_Text.length()==0){ //this line
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
}
Regarding the cancelable, you need to builder.cancelable(false); and whenever needed, builder.dismiss().
builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
if (m_Text.isEmpty()) {
input.setHint("Morate upisati razlog storniranja kartice (naloga)!");
input.setError("Upišite razlog storniranja!");
input.setFocusable(true);
}else{
dialog.dismiss();
}
}
});
Use TextUtils.isEmpty() to check the empty string.
Try this:
builder.setPositiveButton("Spremi", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if(TextUtils.isEmpty(input.getText().toString())) {
// Empty
input.setError("Input is required!");
} else {
// Do something...
}
}
});
Hope this will help~

Alert Confirmation dialog Android

guys I've been trying to solve this problem but I couldn't
I want when the user click on the btn_delete he will get a message to insure the delete (Yes or No), I've tried a lot of methods but I don't know exactly what's the problem, I'm new in Android programing so forgive me for my stupid questions, here is my Java code :
public void onDeleteClick(View v) {
int i = Integer.parseInt((String)v.getTag());
Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
_dbAdapter.deleteAddress(address.Id);
_GetAddresses();
}
Replace the onDeleteClick method with the following method:
public void onDeleteClick(View v) {
int i = Integer.parseInt((String)v.getTag());
AlertDialog.Builder alert = new AlertDialog.Builder(AddressListActivity.this);
alert.setTitle("Delete");
alert.setMessage("Are you sure you want to delete?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
_dbAdapter.deleteAddress(address.Id);
_GetAddresses();
dialog.dismiss();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}

android alert box with multiple options, how to use those options?

I am a beginner, so if anyone would help me out. I created a list in the dialogue box , now how do i use those options? Like click one and it does something , click another and it does something else.
CharSequence features[] = new CharSequence[] {"Save", "Send", "Something", "Something"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Options");
alertDialog.setItems(features, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Eta chu ma aile",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();
return true;
}
If you know exact position of every item, just compare it with which param.
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// handle "Save" option
} else if (which == 1) {
// handle "Send" option
} ...
}
You can use following code:
Somewhere in another function:
String title = "My Alert Box";
String msg = "Choose Option";
alertfunc(title,msg);
The main alert function:
private void alertfunc(String title, String msg) {
if (title.equals(TASK_VIEW_PROFILE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
//Do something
}
})
.setNegativeButton("Send",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
.setNegativeButton("Something",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
//...and so on
}
}

calling dialog from another dialog repeatedly in amy android app

case 2: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("Socket9 Registeration")
.setMessage("You have been Registered Successfully.Please Login to continue.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
etMobileNo.setText("");
etPassword.setText("");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
etMobileNo.setText("");
etPassword.setText("");
}
}).show();
break;
case 3: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Socket9 Registeration")
.setMessage("Your Code doesn't match.Try Again")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
dismissDialog(2);
showDialog(4);
}}).show();
break;
case 4: new AlertDialog.Builder(this)
.setTitle("Enter Your Registeration Code")
.setView(input)
.setMessage("Registeration code has been delivered on your registered number via sms")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString().trim();
String regsCode2=etFake.getText().toString().trim();
System.out.println("Va "+value+" Reg"+regsCode2);
if(value.compareToIgnoreCase(regsCode2)==0){
validCodeMatch=objCommonServices.sendEvalidCode(etMobileNo.getText().toString().trim(), etPassword.getText().toString().trim(),"OK");
if(validCodeMatch.contains("Code Match")){
showDialog(2);
}
}
else{
dismissDialog(4);
showDialog(3);
}
}}).show();
I have created 3 dialogs tand calling each other on each user input every timeI am also trying to dismissDialog method because earlier i was getting error remove Parent View . How to carry out the process?
case 3: new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Arihant Shopee Registeration")
.setMessage("Your Code doesn't match.Try Again")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { #Override
public void onClick(DialogInterface dialog, int which) {
removeDialog(4);
showDialog(4);
}}).show();
break;
case 4:AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Your Registeration Code");
alert.setMessage("Registeration code has been delivered on your registered number via sms");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
String regsCode2=etFake.getText().toString().trim();
System.out.println("Va "+value+" Reg"+regsCode2);
if(value.compareToIgnoreCase(regsCode2)==0){
validCodeMatch=objCommonServices.sendEvalidCode(etMobileNo.getText().toString().trim(), etPassword.getText().toString().trim(),"OK");
if(validCodeMatch.contains("Code Match")){
showDialog(2);
}
}
else{
removeDialog(3);
showDialog(3);
}
}
});

How to get the response from a modal dialog?

I currently have this:
Builder yesandno = new AlertDialog.Builder(this);
yesandno.setTitle("QuickResponse");
yesandno.setMessage(message);
yesandno.setPositiveButton("YES", null);
yesandno.setNegativeButton("NO", null);
yesandno.show();
How should I go by setting an event listener that will capture if the user clicked YES or NO?
When you call setPositiveButton() and setNegativeButton() instead of passing in null you should pass in a DialogInterface.OnClickListener.
For example:
yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//User clicked yes!
}
});
Just do something like:
yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked yes
}
});
yesandno.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked no
}
});
and do whatever you want in the button callbacks.

Categories