I have developed an Android Service which will run in the background. The Service want to accept Yes or No confirmation from the user at an event(say when receive an SMS).
Its working fine; the Yes or No question will be shown to user. But i want the input from (press YES or NO) user without exit from the alert(exit by press on the Mobile Back button or Home button etc).
Please help me how it can be possible.
Below the code I am using;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
//ctx is the Context
builder.setTitle("Emergency!");
String txt="Do you want to accept?";
builder.setMessage(txt);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
///dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
just add this in you AlertDialog
builder.setCancelable(false);
hope you are useing android.support.v7.app.AlertDialog;
You can disable back press while the dialog is shown (or possibly, send a "No" response when back button is pressed) with dialog.setOnKeyListener
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Unfortunately you can't override Home button press on Android. You can, however, react to an activity being sent to the background via Home button by implementing onUserLeaveHint in the activity hosting your dialog.
Edit: You may also want to disable dismissing the dialog by touching outside (which is the default behavior) by doing
dialog.setCanceledOnTouchOutside(false);
Related
I'm a beginner in java and I have a question for you do you know how I can create the button of exit? This button can ask me before I close the application "Do you want to close this application? or "Are you sure to close it?" I need to do it for my project and I need help. Pls send me some code.
Your question is very broad, however, an AlertDialog is what you are looking for, this is the implementation:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
1.- Search how create a dialog on Android.
Link -> https://developer.android.com/guide/topics/ui/dialogs?hl=es-419
2.- In your view put a back button with onClickListener, insert showDialog created inside.
3.- Implement the override onBackPressed method, insert showDialogCreated inside.
Example onBackPressed
public void onBackPressed() {
var dialog = CustomDialog.newInstance();
dialog.setCancelable(false);
dialog.show(this.getSupportFragmentManager(), "TAG");
dialog.setOnClickListener((whichViewID, tag, args) => {
// Your Logic
// If is pressed positive button call super.onBackPressed else dialog.dismiss()
});
}
I almost finished building an app using android studio. However, I noticed some problems in the testing process.
Here is a brief summary of what I'm doing:
When you click 'button', a popup message appears.
The popup message has two buttons.
Pressing the first button only ends the popup message.
When you press the second button, the current popup message is terminated and additional popup messages appear.
The second popup message has a button and an 'EditText' area for entering the email address.
When you click the button, the popup message ends with performing the Javascript function using the email address entered in the EditText area.
First of all, clicking on the two buttons and entering an email address works normally. But when I do this again for the second time, the app is terminating unexpectedly. I think the 'Builder' part I used to implement the popup message is incorrectly nested, but I can not figure out which part is the problem.
Each pop-up message is implemented using 'Builder' as shown below.
final AlertDialog.Builder builder; //for first popup message
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);}
else {
builder = new AlertDialog.Builder(context);}
final AlertDialog.Builder builder2; //for second popup message
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder2 = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);}
else {
builder2 = new AlertDialog.Builder(context);}
Now the full popup message implementation code. Sorry for the difficulty of reading the code.
button_makingpopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (User.getInstance() != null) {
builder.setCancelable(false) //first popup setting
.setMessage("Do you want to email the measurement data??")
.setPositiveButton("Save and Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //first popup disappear
builder2.setCancelable(false) //second popup setting
.setMessage("Enter your email address below.")
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String email_popup = input.getText().toString();
WebView view2 = new WebView(context);
view2.getSettings().setJavaScriptEnabled(true);
view2.getSettings().setDomStorageEnabled(true);
view2.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:(function() { //some javascript function})()");
}
});
dialog.cancel(); //second popup disappear
}
});
AlertDialog alert_send = builder2.create();
alert_send.show(); //second popup appear
}
})
.setNegativeButton("Save only", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel(); //first popup disappear
}
});
AlertDialog alert = builder.create();
alert.show(); //first popup appear
}
else
//Non-question parts
I would appreciate your advice.
environment: android studio 2.3.3
error message "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
I solved this problem by handling the view.
I refer to this. #kasgoku
Thank you also for those who gave me advice by the comment.
I'm running ussd code from my code. After it returns the results the user will press the ok button to close the the dialog. I want to detect when the user does this.
Let's assume that this is your dialog code:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("dialog's title");
builder.setMessage("dialogs's text");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do stuff when user presses ok button
}
});
builder.setNeutralButton("CANCEL", null); //same here just add the listener
AlertDialog dialog = builder.create();
dialog.show();
//you can use neutralButton as well
There are the 3 basic keys in an Android device.
1. Home button (middle button)
2. Menu button (left most button)
3. Back Button (right most button)
I'm trying to create a code the use will press the Back button and a AlertDialog will appear before return to the previous activity.
How can I handle the back button and do I have to insert it on the onCreate on the Java File of an Activity?
Thank You!
All (4) buttons are handled differently.
The home button and running apps buttons are completely outside your control.
To get the BackButton press, override this function.
#Override
public void onBackPressed(){
//Do Stuff
}
To get the Menu button key press, use the following
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
After you show the user your alert dialog, you can either send a real BackPress to the system using super.onBackPressed(), or you can manually finish() your activity.
#Override
public void onBackPressed(){
AlertDialog.Builder build=new AlertDialog.Builder(currentactivity.this);
build.setMessage("Are you sure you want to return?");
build.setCancelable(false);
build.setTitle("Note");
build.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
Intent i =new Intent(currentactivity.this,anotheractivity.class);
startActivity(i);
//or just type finish();
}
});
build.setNegativeButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
});
AlertDialog alert=build.create();
alert.show();
}
I have a checkbox that is switching state on touch. I am wanting to find out how to ignore the state switch on touch. I have an onClickListener attached to the checkbox and inside of that I am changing the state with "Yes" or "No" buttons.
if(sog.isChecked()){
sog.setChecked(false);
} else {
sog.setChecked(true);
}
Edit: The checkbox is switching when touched and my yes button is also changing state.
Edit 2: I want my dialog to change the state rather than the checkbox doing it on it's own.
Edit 3:
sog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(3);
}
});
builder.setTitle("Checkbox")
.setMessage(
"Are you sure you want to switch checkbox?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
if(sog.isChecked()){
sog.setChecked(false);
} else {
sog.setChecked(true);
setTotalTime();
}
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
return alert;
I am just wanting to add a conformation on changing it from off/on.
Implement an OnCheckedChangeListener, registered on the CheckBox via setOnCheckedChangeListener(). In onCheckedChanged(), if the state of the CheckBox is one where you want additional confirmation, leave the CheckBox alone and pop your dialog. If the user taps a button in the dialog indicating that they do not want to make the change, then manually revert the checked state of the CheckBox to its prior value.
That being said, I agree with Waza_Be's comment. I am not a fan of "pop the confirmation dialog immediately" sorts of scenarios, though occasionally they are necessary. If there is some other sort of confirmation step as part of this UI (e.g., "save" action bar item), and you want to display a confirmation dialog at that point, that would be better, IMHO.