How to Keep an Alertdialog Open After Button Onclick Is Fired

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();

Dismiss AlertDialog.Builder from OnClick

AlertDialog.Builder is best suited for small simple dialog boxes rather than custom dialogs.

The cleanest way to handle custom dialogs is to subclass AlertDialog as a private static class in your context (in this case your activity).

Here is a simplified example:

public class AlertDialogTestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

AlertDialog alert = new myCustomAlertDialog(this);
alert.show();

}

private static class myCustomAlertDialog extends AlertDialog {

protected myCustomAlertDialog(Context context) {
super(context);

setTitle("Profile");

Button connect = new Button(getContext());
setView(connect);
connect.setText("Don't push me");
connect.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// I want the dialog to close at this point
dismiss();
}
});
}

}
}

Android Don't dismiss AlertDialog after clicking PositiveButton

After looking at @Little Child solution, I try to make this. Let us know if this works for you.

    AlertDialog.Builder sayWindows = new AlertDialog.Builder(
MapActivity.this);
final EditText saySomething = new EditText(MapActivity.this);
sayWindows.setPositiveButton("ok", null);
sayWindows.setNegativeButton("cancel", null);
sayWindows.setAdapter(listWords, null);
sayWindows.setView(saySomething);

final AlertDialog mAlertDialog = sayWindows.create();
mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

@Override
public void onShow(DialogInterface dialog) {

Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
// TODO Do something
say = userName + " Says: "+saySomething.getText();
showPosition.setText(say);
}
});
}
});
mAlertDialog.show();

Is there a way prevent AlertDialog from closing with invalid inputs?

Here's how I did it. Technically, it doesn't technically keep the dialog open, it closes it momentarily and re-opens it, but the net result is the same.

class MyAlertDialog implements OnDismissListener, OnCancelListener {
final private EditText editText;
final private AlertDialog alertDialog;
final private EventManager eventManager;
final private CategorySelector categorySelector;

private Boolean canceled;

MyAlertDialog(Context context) {
editText = new EditText(context);
alertDialog = buildAlertDialog(context);
alertDialog.setOnDismissListener(this);
alertDialog.setOnCancelListener(this);
show();
}

private AlertDialog buildAlertDialog(Context context) {
return new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.enter_name))
.setMessage(context.getString(R.string.enter_name))
.setView(editText)
.setNeutralButton(context.getString(R.string.save_text), null)
.setNegativeButton(context.getString(R.string.cancel_text), null)
.create();
}

public void show() {
canceled = false;
alertDialog.show();
}

@Override public void onDismiss(DialogInterface dialog) {
if(!canceled) {
final String name = editText.getText().toString();
if(name.equals("")) {
editText.setError("Please enter a non-empty name");
show();
} else {
doWhateverYouWantHere(name);
}
}
}

@Override public void onCancel(DialogInterface dialog) {
canceled = true;
}
}

AlertDialog - do not dismiss on item click

To prevent dialog from dismissing on item click you can use AdapterView.OnItemClickListener instead of DialogInterface.OnClickListener.

Like this:

dialogBuilder.setAdapter(adapter, null);
...
AlertDialog dialog = dialogBuilder.create();
alertDialog.getListView().setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do your stuff here
}
});

How to supply the right context that will make the button of an AlertDialog to work

I hope you have added these two lines in your manifest file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Ok as I don't have rights to comment yet, I'm writing it in answer section.Below answer will surely solve your problem ,please try it:

 @Override
protected void onCreate(Bundle savedInstanceState) {
//The usuals
Log.d(TAG, "onCreate called");

showDialog();

if (NetworkCheck.isAvailableAndConnected(MainActivity.this)) {

getData();

} else {
internetDialog.show();
}

}

private void showDialog() {
internetDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.alert_titl)
.setCancelable(false)
.setIcon(R.mipmap.ic_launcher)
.setMessage(R.string.alert_mess)
.setPositiveButton(R.string.alert_retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!NetworkCheck.isAvailableAndConnected(MainActivity.this)) {
if (internetDialog != null && internetDialog.isShowing()) {
internetDialog.dismiss();
internetDialog = null;
showDialog();
internetDialog.show();
}
} else {
getData();
}

}
})
.setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();

}
})
.create();

}

//This method will get data from the web api

private void getData(){

Log.d(TAG, "getData called");
//Showing progress dialog
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setCancelable(false);
mProgressDialog.setMessage(this.getResources().getString(R.string.load_post));
mProgressDialog.show();

//Creating a json request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigPost.GET_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "onResponse called");
//Dismissing the progress dialog
if (mProgressDialog != null) {
mProgressDialog.hide();
}
/*progressDialog.dismiss();*/

//calling method to parse json array
parseData(response);

}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (mProgressDialog != null) {
mProgressDialog.hide();
}

}
});

//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);

//Adding request to the queue
requestQueue.add(jsonArrayRequest);

}


Related Topics



Leave a reply



Submit