Android Dialog, keep dialog open when button is pressed
Yes, you can. You basically need to:
- Create the dialog with DialogBuilder
- show() the dialog
- Find the buttons in the dialog shown and override their
onClickListener
So, create a listener class:
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
// Do whatever you want here
// If you want to close the dialog, uncomment the line below
//dialog.dismiss();
}
}
Then when showing the dialog use:
AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));
Remember, you need to show the dialog otherwise the button will not be findable. Also, be sure to change DialogInterface.BUTTON_POSITIVE to whatever value you used to add the button. Also note that when adding the buttons in the DialogBuilder you will need to provide onClickListeners
- you can not add the custom listener in there, though - the dialog will still dismiss if you do not override the listeners after show()
is called.
How to keep an alertdialog open after button onclick is fired?
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();
How to keep Dialog open
You can stop setting listener while building AlertDialog and set listeners null for positive and negative buttons and handle clicks by yourself.
You can change your code this way:
private void openDialog() {
LayoutInflater inflater = LayoutInflater.from(TrueAct.this);
View subView = inflater.inflate(R.layout.newdialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enter New");
builder.setView(subView);
blEntryExistToday = true;
builder.setPositiveButton("ADD", null);
builder.setNegativeButton("CANCEL", null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!blEntryExistToday) {
//CLOSE DIALOG
dialog.dismiss();
} else {
tvM.setText("An entry for this day already exist!");
//DO NOT CLOSE DIALOG
}
}
});
Button negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_LONG).show();
//CLOSE THE DIALOG
dialog.dismiss();
}
});
}
});
alertDialog.show();
}
Is it possible to keep a Dialog open after clicking the Neutral Button?
From what I know, it is not possible without extending the Dialog
class. However, with the functionality that you have it may be easier and better just to put it in its own Activity
and use a Dialog theme
. All you have to do is put your code into a new Activity
for this and in your manifest
use the dialog theme
<activity
android:name="com.your.package.YourClassName"
android:label="YOurLabel"
android:theme="@android:style/Theme.Dialog" >
</activity>
This will give the look and feel of a Dialog
while being contained in its own Activity
Here is a SO answer on extending Dialog. I haven't looked through it all but looks like it may give you what you need if you choose this option.
how to keep alert dialog open if user not provide the whole information
You did some wrong validation
if(etNumber_Bottles.length()==0 && etRequired_At.length()==0 && etContact_number.length()==0)
{
Toast.makeText(mContext, "Please Enter All Values....", Toast.LENGTH_SHORT).show();
return;
}
as this validation works only when all the field empty instead of && operator you should use OR operator and take the string value of editText dont use length value direct on edit text first type cast to string than get the length. You can take use length method or equals method for this like below.
You can use this.
if(etNumber_Bottles.getText().toString().tolength()==0 || etRequired_At.getText().toString().length()==0 || etContact_number.getText().toString().length()==0)
{
Toast.makeText(mContext, "Please Enter All Values....", Toast.LENGTH_SHORT).show();
return;
}
Or by equals method:
if(etNumber_Bottles.getText().toString().equals("") || etRequired_At.getText().toString().equals("") || etContact_number.getText().toString().equals(""))
{
Toast.makeText(mContext, "Please Enter All Values....", Toast.LENGTH_SHORT).show();
return;
}
Prevent multiple button presses on AlertDialog
I have a feeling you have a style resource for this dialog
Just add
<item name="android:splitMotionEvents">false</item>
to your dialog style. It will prevent multi-touch for all dialogs using it.
Prevent Android activity dialog from closing on outside touch
This could help you. It is a way to handle the touch outside event:
How to cancel an Dialog themed like Activity when touched outside the window?
By catching the event and doing nothing, I think you can prevent the closing. But what is strange though, is that the default behavior of your activity dialog should be not to close itself when you touch outside.
(PS: the code uses WindowManager.LayoutParams)
How to prevent AlertDialog box getting dismissed when clicked outside the dialog box?
Here is a simple dialog which cannot be canceled by clicking outside it:
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// create dialog
val builder = AlertDialog.Builder(this).apply {
setPositiveButton("Ok",
DialogInterface.OnClickListener { dialog, id ->
// User clicked OK button
})
}
//This will make dialog unCancelable
val alertDialog: AlertDialog = builder.setCancelable(false).create()
// show dialog
alertDialog.show()
}
}
Related Topics
Why Is Listview.Getcheckeditempositions() Not Returning Correct Values
Chaining Requests in Retrofit + Rxjava
Select Distinct Value in Android SQLite
How to Draw Dashed Polyline with Android Google Map Sdk V2
Flutter - How to Pass Custom Arguments in Firebase Dynamic Links for App Invite Feature
Relocate Google Logo in Mapview
How to Customize the Action Mode's Color and Text
Android: How to Measure Total Height of Listview
How to Fill Color in Image in Particular Area
Android Sdk Tools Revision 22 Issue
Android: How to Enable/Disable an Activity's Intent Filter Programmatically
Javamail API in Android Using Xoauth
Android Systemui Glitches in Lollipop
Add an Array of Buttons to a Gridview in an Android Application