This question already has answers here:
How to prevent a dialog from closing when a button is clicked
(21 answers)
Closed 3 years ago.
The subject kinda says it all.. I'm requesting a PIN code from the user, if they enter it, click the OK Positive Button and the PIN is incorrect I want to display a Toast but keep the dialog open. At the moment it closes automatically.. Sure this is very trivial thing to correct but can't find the answer yet.
Thanks..
You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:
/*
* Prepare the alert with a Builder.
*/
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setNegativeButton("Button", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {}
});
this.alert = b.create();
/*
* Add an OnShowListener to change the OnClickListener on the
* first time the alert is shown. Calling getButton() before
* the alert is shown will return null. Then use a regular
* View.OnClickListener for the button, which will not
* dismiss the AlertDialog after it has been called.
*/
this.alertReady = false;
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
if (alertReady == false) {
Button button = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
alertReady = true;
}
}
});
Part of this solution was provided by http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#
Build a custom dialog with a EditText with the attribute android:password="true" a button, then manually set onClick listener the button, and explicitly choose what to do in it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="180dip"
android:digits="1234567890"
android:maxLength="4"
android:password="true"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/Accept"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Accept"/>
</LinearLayout>
</LinearLayout>
Then when you want it to pop up:
final Dialog dialog = new Dialog(RealizarPago.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("PIN number:");
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.Accept);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(password_wrong){
// showToast
} else{
dialog.dismiss();
// other stuff to do
}
}
});
dialog.show();
You can set an OnClickListener as follows to keep the dialog open:
public class MyDialog extends AlertDialog {
public MyDialog(Context context) {
super(context);
setMessage("Hello");
setButton(AlertDialog.BUTTON_POSITIVE, "Ok", (new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// this will never be called
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ok) {
// do something
dismiss();
} else {
Toast.makeText(getContext(), "when you see this message, the dialog should stay open", Toast.LENGTH_SHORT).show();
}
}
});
}
}
You can just continue using the dialog you already have, just put an if clause in the onClick() saying
if(pin_check_method){ //pin_check_method should be a boolean returned method
//close the Dialog, then continue
}
else{
//dont put the dialog.dismiss() in here, put instead
Toast.makeText(getApplicationContext(),"Invalid pin, please try again",Toast.LENGTH_LONG).show();
}
Now, to use this code, simply invoke text.setText(""); and put in the text you want here
common error is that when you type in:
TextView text = (TextView) findViewById(R.id.dialog);
you miss that it needs to actually be
dialog.findViewById
and this is regardless of what the name of the dialog is, in my example it just happens to be the same name.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="#+id/text"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"/>
<Button android:text="Continue"
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/text">
</Button>
</RelativeLayout>
Try this:
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Do something
}
});
}
});
alertDialog.show();
Source: Prevent Alertdialog from closing after button click
Hope This Helps! Good Luck!
Same problem for me in a FragmentDialog. Here's my criminal/elegant solution:
Remove all buttons from the dialog (positive,negative,neutral). Add your buttons from the xml.eg.:
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button_cancel"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:text="#android:string/cancel"
android:layout_gravity="left"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/button_ok"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:text="#android:string/ok"
android:layout_gravity="right"
/>
</LinearLayout>
And then in your code handle it with:
view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view2) {
if (wannaClose)
dismiss();
else
//do stuff without closing!
}
});
where view is the view assigned to the dialog!
Related
When you click on the image button, pop up notification pops up. How do I customize the "ok" and "cancel" button to instead of using the default look of the buttons, I want to use my own custom ImageButtons as "ok" and "cancel".
Here's my code for pop up notification.
public class Notifications extends AppCompatActivity {
ImageButton Notifications;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
Notifications = (ImageButton) findViewById(R.id.AllowNotifications);
Notifications.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Notifications.this);
builder.setCancelable(false); //False= ONLY way to exist notification is by clicking NO
//True= Exit notification by clicking anywhere on screen outside of notification box.
builder.setTitle("Here is the alert dialog");
builder.setMessage("Here is my message thing");
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int WhichButton) {
dialog.cancel();
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
}
Here's the default pop up notification with the above code:
So instead of there being an "ok" and "cancel" in red color, I want to put the "ok" and "cancel" as my own custom image buttons and I'd want to change the color from red to something else. How do I go about doing this inside the Pop Up notification?
As the documentation says, in the Creating a Custom Layout session, you can create a custom layout and inflate it at your Dialog.
To use another button than the one create by the AlertDialog.Builder you will need to handle the click listener of them.
This is the layout I created to test the solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="#+id/dialogTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is the alert dialog"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/dialogSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is my message thing"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp">
<Button
android:id="#+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#android:color/transparent"
android:text="OK"
android:textColor="#android:color/holo_red_light"/>
<Button
android:id="#+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_toStartOf="#id/positiveButton"
android:background="#android:color/transparent"
android:text="Cancel"
android:textColor="#android:color/holo_red_light"/>
</RelativeLayout>
</LinearLayout>
And the code to make it run:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.test, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
TextView title = (TextView) promptView.findViewById(R.id.dialogTitle);
TextView subtitle = (TextView) promptView.findViewById(R.id.dialogSubtitle);
title.setText("My new Custom Dialog");
subtitle.setText("With everything that I want");
Button positive = (Button) promptView.findViewById(R.id.positiveButton);
Button negativeButton = (Button) promptView.findViewById(R.id.negativeButton);
positive.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// btnAdd1 has been clicked
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// btnAdd2 has been clicked
}
});
alertD.setView(promptView);
alertD.show();
This is an screenshot of how it looks like in my phone. Feel free to change the layout in the way it better fits your needs.
Thanks to Vikram that explains it very well in this answers for other question, but I thought that a specific code for your question would be better.
If you want to customize everything, the look of the dialog, add your own buttons, TextViews etc. - you need to make a class that extends DialogFragment and implements View.OnClickListener and you need to create your own layout with two custom made buttons for that. Give them ids and set OnClickListeners
As typed in: https://developer.android.com/reference/android/app/DialogFragment.html
public static class MyDialogFragment extends DialogFragment implements View.OnClickListener {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment, container, false);
v.findViewById(R.id.btn_ok).setOnClickListener(this);
return v;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_ok:
// do something
break;
default:
break;
}
}
}
And from your Activity you do:
void showDialog() {
// Create the fragment and show it as a dialog.
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
This image took from Deep Relax app from Play Store:
How do I create a popup menu? I am creating an app I want to ask the user to provide feedback?
Try this use custom dialog for this purpose:
Create layout like this custom_dialog_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textColor="#000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting," />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:id="#+id/btnNotNOw"
android:text="not now"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#null"
android:text="Never"
android:id="#+id/btnNever"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#null"
android:text="sure"
android:id="#+id/btnSure"
android:textColor="#040d28" />
</LinearLayout>
</LinearLayout>
Now create dialog using below code:
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Button btnNotNOw, btnNever, btnSure;
btnNotNOw = (Button) dialog.findViewById(R.id.btnNotNOw);
btnNever = (Button) dialog.findViewById(R.id.btnNever);
btnSure = (Button) dialog.findViewById(R.id.btnSure);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
dialog.show();
btnNever.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
btnNotNOw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
btnSure.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
You can create it by Custom Dialog :
Custom Dialog :
For this you have to create one xml file and have to attached.
final Dialog dialog1 = new Dialog(LabCheckOutActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setCancelable(true);
dialog1.setContentView(R.layout.dialog_patient_details);
dialog1.show();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Top Heading");
// Setting Dialog Message
alertDialog.setMessage("Message");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Sure",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Not Now", Toast.LENGTH_SHORT).show();
}
});
// Setting Netural "Cancel" Button
alertDialog.setNeutralButton("Never", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Never",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
Check this
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this, AlertDialog.THEME_TRADITIONAL);
builder.setMessage("Positive,Negative and Neutral Button")
builder.setCancelable(false);
builder.setTitle("Custom Dialog");
// Set the positive/yes button click listener
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the negative/no button click listener
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();
I have accept and reject button, both to dismiss the dialog box. I also have a condition where the dialog will be removed if there is no action by the user, but it is redrawn after being removed. Upon thorough investigation I found out the dialog box was redrawn right after it is removed. This is my code:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, null);
builder.setView(dialoglayout);
builder.setTitle("REQUEST");
builder.setMessage("Would you like to accept?");
//builder.show();
dia = builder.create();
dia.show();
spRequest.play(spSoundId, 1, 1, 1, 15, 1);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
dia.dismiss();
}
}, 7000); //the alert will play for 7 seconds and stop
//dia.show();
Button accept = (Button) dialoglayout.findViewById(R.id.bAcceptRequest);
accept.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//mDialog.cancel();
dia.dismiss();
spRequest.release();
}
});
Button reject = (Button) dialoglayout.findViewById(R.id.bDeclineRequest);
reject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//mDialog.cancel();
Log.e("caution", "dismiss reached");
dia.dismiss();
spRequest.release();
}
});
And this is my custom dialog xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/custom_dialog_background">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/bAcceptRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/acceptrequest"
android:background="#drawable/blue_button"
/>
<Button
android:id="#+id/bDeclineRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/rejectrequest"
android:background="#drawable/blue_button"
/>
</LinearLayout>
Can someone explain to why the dialog is being redrawn and how to stop that behavior?
I have a button with id "ok" in my dialog.
Partial - Dialog Layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:layout_below="#+id/licenseTypeLayout">
<Button
android:id="#+id/ok"
style="#style/agreement_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
In activity:
Dialog dialog;
#OptionsItem(R.id.action_about)
boolean displayPopup() {
dialog = new Dialog(this);
dialog.setContentView(R.layout.about_dialog);
dialog.setTitle(R.string.app_name);
dialog.show();
Button btn=(Button)findViewById(R.id.ok);
//btn remains empty
return true;
}
How would I write onClick() event for this button?
To initialize the Button, you need to use the Dialog object
Button btn = (Button) dialog.findViewById(R.id.ok);
Then set OnClickListener to the Button using setOnClickListener() as follows...
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//write your code here
}
});
I would guess you make the method:
public void closeDialog(View v){
// Do what you want to do here (event handler)
}
?
here I have a custom dialog with background 2 ImageButton inside it.
the problem is, when I try to set onclick listener to that buttons, the program will return NullPointerException. I don't know why is this happen. how to assign operation to button inside dialog anyway??
pause menu xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="wrap_content" android:background="#drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal">
<TableLayout android:layout_width="wrap_content" android:id="#+id/tableLayout1" android:layout_height="wrap_content">
<ImageButton android:src="#drawable/pause_button_option" android:layout_width="wrap_content" android:background="#drawable/pause_button_option" android:layout_height="wrap_content" android:id="#+id/btn_pause_option"></ImageButton>
<ImageButton android:src="#drawable/pause_button_quit" android:layout_width="wrap_content" android:background="#drawable/pause_button_quit" android:layout_height="wrap_content" android:id="#+id/btn_pause_quit"></ImageButton>
</TableLayout>
</LinearLayout>
dialog code
Dialog pauseMenu = new Dialog(this, R.style.NewDialog);
pauseMenu.setContentView(R.layout.pause_menu);
ImageButton quit = (ImageButton)findViewById(R.id.btn_pause_quit);
quit.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
TestActivity.this.finish();
}
});
return pauseMenu;
the code is returning error in line
quit.setOnClickListener();
ImageButton quit =
(ImageButton)findViewById(R.id.btn_pause_quit);
should be
ImageButton quit = (ImageButton)pauseMenu.findViewById(R.id.btn_pause_quit);
This happens because findViewById is invoked for the activity, and it doesn't have btn_pause_quit button in it's layout. But your dialog has.
U can use this custom dialog and onclicklistener..
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.OkButton);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}
}
I think your onClickListener should be DialogInterface.OnClickListener