Please, i would like to show back details after the user must have input something, back on alert dialog box in Android studio. I used this code below:
editText = (EditText) findViewById(R.id.my_edit_txt);
editText.getText().toString();
But it doesn't show on the confirmation dialog box I created.
It looks like you didn't set the text of your AlertDialog, but this is just an assumption because there is not enough code in your question. Calling editText.getText().toString() does not do anything but return a String. It does not assign it to anything. An example with an AlertDialog would be the following:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(editText.getText().toString());
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
I've took this example from Android Developers and modified it so that it includes the text of your EditText. This code should work because you not only call the toString() method but also assign it's return value to the AlertDialog's message property.
This is my entire code for the alert dialog box:
public void alertdialog(View view){
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder cfmalt = new AlertDialog.Builder(Dashboard.this);
//cfmalt.setMessage("Do you want to quit?").setCancelable(false);
//editText.getText().toString();
cfmalt.setMessage(editText.getText().toString()+"\n"+ vol_edit2.getText().toString());
cfmalt.setMessage(dt.getMonth())
//cfmalt.setMessage("Name:").setMessage(vol_edit2.getText().toString());
cfmalt.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
cfmalt.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
Related
I'm working on a small class which can generate Alert dialog boxes. The constructor of the class looks like this:
void popupMessage(String title, String message, String pText, String nText, boolean cancelable) {
setPopupResult(999);
AlertDialog.Builder dialog = new AlertDialog.Builder(currentActivity);
dialog.setMessage(message).setCancelable(cancelable);
dialog.setNegativeButton(nText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setPopupResult(0);
}
});
dialog.setPositiveButton(pText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setPopupResult(1);
}
});
AlertDialog alert = dialog.create();
alert.setTitle(title);
alert.show();
}
as you see based on pressing the yes or no button the code sets the value of a private variable to 0 or 1 which can be accessed by a getter method. (the value is set to 999 at the top, this indicates that the user did no press anything yet)
The problem I'm facing is that from in the calling activity I somehow should be able to capture when the popupResult variable changes from 999 to either 0 or 1. How can I do that?
(I could be wrong handling the Alert dialog like this, feel free to educate me)
Since the user's clicking on your dialog buttons is asynchronous to when you're showing the dialog, one way to do it, would be to provide some kind of callback to your method, that is called when the buttons are clicked.
Example:
/* define this inside your dialog class */
public interface Callback {
void onOkClicked();
void onCancelClicked();
}
void popupMessage(String title, String message, String pText, String nText, boolean cancelable, Callback callback) {
...
/* positive button clicklistener, for negative button, use callback.onCancelClicked() */
public void onClick(DialogInterface dialog, int which) {
callback.onOKClicked();
}
...
}
/* Using the method */
popupMessage(..., new Callback() {
void onOKClicked() {
/* do something when OK was clicked */
}
void onCancelClicked() {
/* do something when Cancel was clicked */
}
});
I see that you already have the context of the activity in the variable currentActivity. Create the method setPopupResult() in your activity like this:
public void setPopupResult(int x) {
// your code goes here
}
and in popupMessage(), if the class of your activity is MainActivity:
dialog.setNegativeButton(nText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity) currentActivity).setPopupResult(0);
}
});
dialog.setPositiveButton(pText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity) currentActivity).setPopupResult(1);
}
});
I am making an ALert Dialog with custom EditText Field.
I made a View variable and then associated it with my custom EditText field.
requestView = inflater.inflate(R.layout.send_request,null);
Then I added that view to my AlertDialog
alert.setView(requestView);
And after that I added the onClick Method To My Button to perform the alert Dialog action..
chatRequestbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
request = requestMsg.getText().toString();
send();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
}
});
It worked correctly. But after pressing cancel option on alert dialog when I press the button again to perform the alert dialog option.
It crashes with the following error.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
at android.view.ViewGroup.addView(ViewGroup.java:4258)
at android.view.ViewGroup.addView(ViewGroup.java:4230)
at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
at android.support.v7.app.AlertController.setupView(AlertController.java:463)
at android.support.v7.app.AlertController.installContent(AlertController.java:226)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
at android.app.Dialog.show(Dialog.java:294)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
at com.buckydroid.anonchat.User$3.onClick(User.java:86)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I though making the view null and adding the view again while clicking on the button will fix the issue. But same problem again and again..
If you want to use an existing View, use this.
alert.setOnDismissListener(new OnDismissListener(){
((ViewGroup)requestView.getParent()).removeView(requestView);
});
I converted the above code from Kotlin to Java by hand, plz check before use.
Your problem is here:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(requestView);
In this case alertis not the dialog, but a builder. So every time when you're trying to show it - it rebuilds this dialog and trying to add same view for it - requestView. Because it is cached in the builder. To fix it - move
requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);
to your OnClick method where you're showing the dialog. So it should look like this:
chatRequestbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);
alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
request = requestMsg.getText().toString();
send();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
}
});
You want to set an on dismiss listener. I did something like:
The dialog should set an on Dismiss listener
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
((ViewGroup)requestView.getParent()).removeView(requestView);
}
});
I have a NeutralButton that should cause an AlertDialog box I have to disappear. This issue is, when the AlertDialog box comes up, the text set appears just fine but the NeutralButton doesn't show at all. I tried it as a Positive and Negative button, but no difference was made. Here's the code:
Button infoG1;
infoG1 = (Button) findViewById(R.id.iG1);
infoG1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder info = new AlertDialog.Builder(game1.this);
info.setMessage("Text here");
info.show();
info.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
});
I would have expected a button with text "Ok" to appear at the bottom of the AlertDialogue box, which causes the box to disappear once the user presses it.
You shouldn't call setNeutralButton(...) after your show() method. You are doing this, which is causing the problem.
Instead of this:
dialog.show();
dialog.setNeutralButton(...);
Do this:
dialog.setNeutralButton(...);
dialog.show();
Note that you should put everything above your dialog.show(). Otherwise you will have problems.
try this
Button infoG1;
infoG1 = (Button) findViewById(R.id.iG1);
infoG1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder info = new AlertDialog.Builder(game1.this);
info.setMessage("Text here");
info.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
info.show();
}
});
I am trying to include an AlertDialog builder within a method that prompts for a pin code and when the positive button is pressed, checks it against a database value and returns a true or false value to the method caller.
For example: Adding/editing/deleting a user task requires a pin code. I don't want to generate a different AlertDialog for all three (and more) of these actions. I want to wrap the following code within a TaskService class that I can call from any activity, and react based on the result from within that activity.
So TaskService.java would have:
public boolean isCorrectPin(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
final EditText editText = new EditText(context);
builder.setView(editText);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (editText.getText().toString()) == getPinCode(){
//return true
}
}
});
builder.show();
}
and OpenTaskAdapter.java would have:
public void onBindViewHolder(ViewHolder holder, int position){
holder.btnMarkAsComplete.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (service.isCorrectPin(v) {
//complete task
}
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (service.isCorrectPin(v) {
//delete task
}
}
});
It's important to note that these two button listeners could be in totally different activities.
You can create your own method to generate dialog with listener:
public void isCorrectPin(Context context, String title, String message, String btnPositive, final DialogSingleButtonListener dialogSingleButtonListener) {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle(title);
dialogBuilder.setMessage(message);
dialogBuilder.setPositiveButton(btnPositive, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (editText.getText().toString() == getPinCode()){
dialogSingleButtonListener.onButtonClicked(dialog);
}
}
});
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
And the listener class:
public interface DialogSingleButtonListener {
public abstract void onButtonClicked(DialogInterface dialog);
}
And use it like:
service.isCorrectPin(context, title, message, btnPositive
new DialogSingleButtonListener() {
#Override
public void onButtonClicked(DialogInterface dialog) {
//code here is only called if they entered a correct pin.
}
}
);
A dialog can't "return" a value in the way that it looks like you're expecting. A dialog can make changes to some other object, but you can't have a bit of code block on it and wait for the user to finish interacting with it.
Instead, you'll need to set up listeners for when the prompt dialog is dismissed or buttons or clicked, or whatever other event signals that you have what you need from it. Those listeners can then read the data gathered and set by the dialog.
this is how i'm doing :
public Boolean showAlert(String message)
{
action = false;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(HAActivity.this);
// Setting Dialog Title
alertDialog.setTitle(getString(R.string.app_name));
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
action = true;
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
action = false;
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
return action;
}
and calling function like this :
//activity in which you create function
if (Activity.showAlert("Do you really want to delete ??"))
{
//delete it anyway.
}
I've got a really strange problem where on some KitKat devices, my simple yes/no AlertDialog will appear behind the current fragment and not in the foreground. The reason I say the dialog appears behind the current fragment is because the dialog appears in the foreground only after I rotate the device. The app has a MainActivity that switches between different fragments that take up most of the screen.
MainActivity.java
#Override
public void onBackPressed() {
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.exit_confirm_summary))
.setTitle(getString(R.string.exit_confirm_title))
.setCancelable(true)
.setPositiveButton(getString(R.string.ok),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
})
.setNegativeButton(getString(R.string.cancel),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
After doing some research I found that it is best to use DialogFragment when using Fragments in your app, so I changed my code to this:
MainActivity.java
#Override
public void onBackPressed() {
AlertDialogFragment adf = new AlertDialogFragment();
adf.setRetainInstance(true);
adf.show(getFragmentManager(), "dialog");
}
AlertDialogFragment.java
public class AlertDialogFragment extends DialogFragment {
public AlertDialogFragment() {}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setTitle("Alert DialogFragment")
.setMessage("AlertDialogFragment Test")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
}).create();
}
}
However, the effect is still the same. The dialog should appear when I press the back button, but is only visible after I press the back button and then rotate the device. It also becomes visible after I go home and come back into the app. I've noticed it only happens on a few devices but I'd like to get rid of this problem for good.
Note: this behavior happens for all dialogs in the app, not just this one.
Anyone have any ideas what is going on?