i want to have in my application an alertdialog, that has its message updated everytime it is showed.
This is because the dialog box value depends on some values on the application.
Now i tried to use the showDialog method:
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
showDialog(RESULT_DIALOG);
return false;
}
But once the dialog is created, it doesn't change the message (i know that if the dialog is created, it use the started version).
My onCreateDialog method code is:
public Dialog onCreateDialog(int dialogId) {
AlertDialog dialog;
switch(dialogId) {
case RESULT_DIALOG:
// do the work to define the pause Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(localTv.getText())
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
There is a way to update the content of the AlertDialog.
Actually i create a new dialog box every time the onTouch event is called. But i'm not sure that it is the cleanest way to solve that problem.
Any idea?
Thanks :)
You have to use onPrepareDialog method:
#Override
protected void onPrepareDialog ( int id, Dialog dialog ) {
switch ( id ) {
case RESULT_DIALOG:
AlertDialog alertDialog = ( AlertDialog ) dialog;
alertDialog.setMessage( localTv.getText() );
break;
}
super.onPrepareDialog( id, dialog );
}
From http://developer.android.com/guide/topics/ui/dialogs.html :
Before the dialog is displayed, Android also calls the optional
callback method onPrepareDialog(int, Dialog). Define this method if
you want to change any properties of the dialog each time it is
opened. This method is called every time a dialog is opened, whereas
onCreateDialog(int) is only called the very first time a dialog is
opened. If you don't define onPrepareDialog(), then the dialog will
remain the same as it was the previous time it was opened. This method
is also passed the dialog's ID, along with the Dialog object you
created in onCreateDialog().
You can always change the dialog using onPrepareDialog or you can remove the dialog (so it will always pass through onCreateDialog) setting the onDismiss (dialog.setOnDismiss) to remove the dialog id (removeDialog(id)).
Related
I need to create a popup window programmatically, with a scrollview is this possible? i need to do everything in java side.
I was using a alert dialog but maybe is better a popup window, but didn't find much information on how to do it programatically.
i was using this code for the alert dialog
AlertDialog.Builder alert = new AlertDialog.Builder(OFActivityA.this);
alert.setTitle("Something");
alert.setText("fe");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//what you need to do after click "OK"
}
});
alert.show();
i need to do everything in java side., not really - you can create your custom dialog with a custom layout and have any kind of layout that you would like to have.
For example, create dialogClass:
public class ProgressDialog extends Dialog {
public ProgressDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.progress_dialog); //this is your layout for the dialog
}
}
And all you need to do to show your dialog is call those line:
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.show(); // this line shows your dialog
You can put all your logic into the class using java as you want to only that now you can control your layout and how it looks in easier way.
I have a popup window in my activity.
Whenever I change the screen orientation to landscape, the popup disappears.
Why is that, and how can I keep the popup visible?
try below code:-
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.i("orientation", "Orientation changed to: Landscape");
else
Log.i("orientation", "Orientation changed to: Portrait");
}
see below link for more info:-
How to keep Popup window opened when orientation changes at run time in Android?
When orientation changes the activity will restart.. So normally the popup window calls again.. In any case if it gone try to call it within onCreate.
Or check the orientation change and take necessary recalls.
if(getResources().getConfiguration().orientation == getResources()
.getConfiguration().ORIENTATION_LANDSCAPE){
// put some flag
}else if(getResources().getConfiguration().orientation != getResources()
.getConfiguration().ORIENTATION_PORTRAIT) {
// change the flag
}
If you put your code fragments may I can help you
You need to use managed Dialogs. Rather than
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.rule_edit_choose_action));
builder.setAdapter(new ArrayAdapter(this, R.array.dummyValues), null);
builder.show();
you should use something like
myActivity.showDialog(0);
and then implement onCreateDialog() in your Activity. Your activity will then manage the dialog and re-show it when you re-orientate and it's closed. If you need to change your dialog every time it is shown, implement onPrepareDialog() also - the Activity will give you access to the Dialog just before it is shown so you can update it (with a custom message, for instance).
There's lots of info here:
http://developer.android.com/guide/topics/ui/dialogs.html
As #Ciril said, your issue is that your Activity is restarted when you re-orientate. You could always fix your activity orientation to portrait or landscape if that is suitable for your app. That would prevent it from restarting.
most likely you use AlertDialog for your popups, something along the lines of:
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.popup_title);
builder.setMessage(R.string.popup_message);
builder.setPositiveButton(R.string.yes, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
});
builder.show();
this is bad, because your Activity has no idea there's a popup dialog, and when you change screen orientation, the Activity is restarted with the new parameters, and your popup is gone.
to avoid this you'd better use ShowDialog() to display your popups. to make it work, you need to override onCreateDialog() :
// Called to create a dialog to be shown.
#Override
protected Dialog onCreateDialog(int id, Bundle bundle) {
switch (id) {
case NEW_DIALOG :
return new AlertDialog.Builder(this)
.setTitle(R.string.popup_title)
.setMessage(R.string.popup_message)
.setPositiveButton(android.R.string.ok, null)
.create();
default:
return null;
}
}
then you'd better override onPrepareDialog() (this is not required, actually):
// If a dialog has already been created, this is called
// to reset the dialog before showing it a 2nd time. Optional.
#Override
protected void onPrepareDialog(int id, Dialog dialog, final Bundle bundle) {
AlertDialog dlg = (AlertDialog) dialog;
switch (id) {
case NEW_DIALOG :
dlg.SetTitle("popup title");
// and maybe something else
}
}
after all preparations you may call ShowDialog(NEW_DIALOG) and you Activity will remember it has a popup laid over on the top, and will recreate it after the orientation change.
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.
Hi stackoverflow friends
I recently faced an issue that how can i disable global search button in android while an alert is shown in the screen.I don't want to disappear the alert box by using search button. I need to user must click the alertbox button and disappears in that way.So I want to disable the search button while alert box is shown. But I can disable the back button using setCancable(false).How can I solve this ?
THanks in advance.
So, Your intention is to provide non-cancelable alert.
Suggesting to set OnDismissListener and just show alert again. It's not very good from visual perspective (alert get closed and opened again).
Below is some obvious example how to achieve such non-cancelable alert (code is inside Acctivity class):
/** reference to our alert */
private AlertDialog alert = null;
/** to indicate if alert dismissed by key */
private boolean alertKeyPressed = false;
#Override
protected void onResume() {
// Say, we need to show alert when activity resumed
if(true/*provide condition to show alert*/) {
showAlert();
}
}
/**
* Show non dismissable alert
*/
private void showAlert() {
if(null == this.alert) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.str_alert_title);
builder.setMessage(R.string.str_alert_text);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
YourActivity.this.alertKeyPressed = true;
dialog.dismiss();
}
});
this.alert = builder.create();
this.alert.setOwnerActivity(this);
this.alert.show();
this.alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// dialog is not allowed to be dismissed, so show it again
YourActivity.this.alert = null;
if(!YourActivity.this.alertKeyPressed) {
showAlert();
}
}
});
}
}
However, I don't think it's the right way to left such alert for the user, sometimes it might be needed for cases like evaluation restriction etc.
Override onSearchRequested in your Activity and have it return false while the dialog is being shown. This should block the request, as per the docs:
You can override this function to force global search, e.g. in
response to a dedicated search key, or to block search entirely (by
simply returning false).
Returns true if search launched, and false if activity blocks it. The
default implementation always returns true.
.setOnKeyListener(new DialogInterface.OnKeyListener()
{
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
//There you catch the key, do whatever you want to do.
//Return true if you handled the key event, so nothing will trigger.
//Return false if you want your activity to handle.
return true;
}
})
Just add the code above to alert dialog builder. Hope this snippet would help. Good luck.
I am trying to create an AlertDialog for a Bluetooth transfer after the transfer notification is touched in an android phone.
I am trying something like this:
Out of the below, I am getting everything - icon, title and two buttons. I am sure I can add other info like From, FileName and others using cursors with the help AlertDialog.Builder properties. I just do not know to get a progress-bar in it. I do not want to use an XML.
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setIcon(R.id.imageFile);
alertDialogBuilder.setTitle("File Transfer");
ProgressDialog progressDialog;
Context mContext = null;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//alertDialogBuilder.setView(progressDialog); //STUCK HERE
//"!" icon and "File Transfer"
//from : device name
//File: <name>
//Type: <type> (<size>)
//Receiving/Sending File
//<%> | Green progress bar
//Hide and Stop buttons
alertDialogBuilder.setPositiveButton("STOP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialogBuilder.setNegativeButton("HIDE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
return alertDialog;
}
I have everything in place except I am not able to figure out how to bring a progress-bar here.
Can I getjust a progress-bar using XML layout and use as
alertDialogBuilder.setView(R.id.what-ever-xml-file)
Or how to create a view for that progress-bar in he Java file itself and put a progressbar in that view and then put that view inside the dialog.
I want sth like this:
You can use Dialog and add ProgressBar on this by using
_dialog.addContentView(view, params) method.