I've got an onCreateDialog method in my activity, which has a case switch to bring up different dialogs that I want to display depending on the request.
I cannot use the showDialog() method from my view because it's not accessible from the context that is passed when the view is created. At least, I can't find a way to access it.
How do I use showDialog from my application's view? Do I need to create a listener? And if so, how? Is there a better method?
Here is my onCreateDialog code that exists in my application's activity:
protected Dialog onCreateDialog(int id) {
AlertDialog alert=null;
switch(id) {
case DIALOG_GAMEOVER_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You died. Play again?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//init();
//oGameState = eGameState.PLAYING;
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
alert = builder.create();
break;
default:
break;
}
return alert;
}
I tried passing a reference to my activity, and I get crashes. Perhaps I am doing it wrong?
In my activity:
// set our MainView as the View
oNebulaMainView = new NebulaMainView(this, this);
setContentView(oNebulaMainView);
In my view:
public NebulaMainView(Context context, Activity oActivity) {
super(context);
// adding the callback (this) to the surface holder to intercept events
getHolder().addCallback(this);
// create the game loop thread
thread = new NebulaMainThread(getHolder(), this);
setFocusable(true);
oActivity.showDialog(DIALOG_GAMEOVER_ID);
}
I may be missing something here, but what is stopping you from just calling:
alert.show()
Just make the alert accessible to the view, and call that form inside your view.
declare alert as an instance variable
Related
I have an Alert Dialog that I am displaying whenever there is a response from the server or an API. It looks like so:
public void showDeveloperDialog(Context context, String responseMessage) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Response");
alertDialog.setMessage(responseMessage);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setCancelable(false);
alertDialog.show();
}
If the dialog gets called from example Activity A and there is a delay with the response prompting the user goes to another activity (Activity B), the app crashes since the context that called it is not in view.
How can I check to ensure that the dialog only gets shown if the initial context is in view?
You could cast the context to an Activity to check if the Activity is finished or not, and only display if its not finished using isFinishing() method
Activity activity = (Activity) context;
if (!activity.isFinishing()) {
// You can now display the dialog
alertDialog.show();
}
After a check demanding the user to switch on internet services and I try to click on a button my app crashes with the error message
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
On this line it crashes, I have tried doing this but not resolved absolutely
if(alert.getContext() != null){
alert.show();
}
This is the complete code
else if (id == R.id.xyz) {
//startActivity(borrowIntent);
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("xyz");
input.setFilters(new InputFilter[] {
// Maximum 2 characters.
new InputFilter.LengthFilter(6),
// Digits only.
DigitsKeyListener.getInstance(),
});
// Digits only & use numeric soft-keyboard.
input.setKeyListener(DigitsKeyListener.getInstance());
input.setHint("xyz");
alert.setView(input);
alert.setPositiveButton("Borrow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if(input.getText().length() == 0)
{
input.setError("xyz is required !");
}
else
{
if(isNetworkAvailable())
{
xyz( input.getText().toString());
}else{
//setContentView(R.layout.main);
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setCancelable(false);
builder.setTitle("xyz");
builder.setMessage("Please enable wifi services");
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
dialog.dismiss();
}
});
AlertDialog alerts = builder.create();
alerts.show();
}//end of block
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
if(alert.getContext() != null){
alert.show(); //crashes at this line
}
}
Please what am I missing?
The problem is on this line:
alert.setView(input);
You added input View that have already parent.
Create new input instance.
according to this post, add this check to remove input from it's parent and readd it:
if(input.getParent()!=null)
((ViewGroup)input.getParent()).removeView(input); // <- fix
alert.addView(input);
Put following line
final AlertDialog alertd = alert.create();
After
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
Following situation can also happen (happened to me):
Sometimes when you use a listview you initialize it with an adapter, which belongs to a certain layout. Now lets say the root view of this layout file is a <LinearLayout> with the id "root_view".
If you register now for a context menu in your activity and create an AlerdDialog.Builder which appears after choosing a certain menu element and initialize it with a layout file, which also has a root element with an id called "root_view" where all elements which belong to your AlertDialog are children of it, then those elements "will not be found". You will not be able to access those elements with findViewById, instead you can only access the elements from the of your list view and you get the same error message at the line where you call builder.show() (or in the case here alert.show()).
So generally it is a good idea to name the ids of your elements in the layout files uniquely, for your project.
I forgot to call create() on the AlertDialog.Builder. When you call show() without calling the create() method, the AlertDialog instance gets created. This worked the first time but then subsequent clicks got the IllegalStateException. As I was calling show() inside of my onClickListener, it was going to create a new AlertDialog instance every time button was clicked.
I have a custom alert dialog. I am currently trying to alter the onclicklisteners for my two buttons. Previously I have used the following code.
builder.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
\\code here which is not relevant to question
}
});
However, now since the dialog has a custom view and custom buttons, I use the following approach.
Button confirm = (Button) windowView.findViewById(R.id.confirmbutton);
Button cancel = (Button) windowView.findViewById(R.id.negatebutton);
cancel.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
}
});
My question is how do I dismiss the dialog within the cancel button listener if I can't access the dialog variable. I want to use the AlertDialog I am already using and do not want a solution with a different type of dialog.
What you need to do, is just keep a reference of the Dialog, then you can call the dismiss method. In my example, i keep the reference as a property.
private Dialog dialog;
#Override
public void onResume() {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LinearLayout llView = new LinearLayout(this);
Button btnDismiss = new Button(this);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
llView.addView(btnDismiss);
adb.setView(llView);
dialog = adb.create();
dialog.show();
super.onResume();
}
It's important keep the reference as a property, cause the reference must be final to be accessible inside the onClick method, and since the dialog it's not created yet, you cant keep the final reference in a method variable, then keep it in a property.
#Override
public void run() {
//Create thread that can alter the UI
AlarmPage.this.runOnUiThread(new Runnable() {
public void run() {
cal = Calendar.getInstance();
//See if current time matches set alarm time
if((cal.get(Calendar.HOUR_OF_DAY) == alarmTime.getCurrentHour())
&& (cal.get(Calendar.MINUTE) == alarmTime.getCurrentMinute())){
//If the sound is playing, stop it and rewind
if(sound.isPlaying()){
ShowDialog();
alarmTimer.cancel();
alarmTask.cancel();
alarmTask = new PlaySoundTask();
alarmTimer = new Timer();
alarmTimer.schedule(alarmTask, sound.getDuration(), sound.getDuration());
}
sound.start();
}
}
});
}
public void ShowDialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("REMINDER!");
alertDialog.setMessage("Turn off alarm by pressing off");
alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT);
}
});
alertDialog.show();
}
I am making a simple alarm clock app that notifies the user. I want to make a alert box that gives the user the option to turn off the alarm when it goes off. I was able to make the alert box, but it only appears in the app not outside of the app. I understand the app has to be in the background running. If I need to show more code or be more specific, just ask please.
Add a line as:
public void ShowDialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("REMINDER!");
alertDialog.setMessage("Turn off alarm by pressing off");
alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
// line you have to add
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
}
check now.
Do not accept answers if they don't address your question, it is misleading.
The accepted answer is not correct, as it will never work outside your application.
Reason:
It requires an activity context not application context.
If you provide application context, your app will crash with IllegalArgumentException- you need to use Theme.AppCompat or their decendents...
If you need functionality as actually stated in the question you have to have a separate activity themed as a Dialog like here
or you can add a custom view to your window using window manager and making it system level alert like here.
Do this create an Activity without ContentView or a View associated with it and call your alertDialog method in your onCreate also remember to set the background of the Activity to Transparent using ColourDrawable
And that activity will look like a dialog or will suit your preference, you can also fall back to Themes so you can set an Activity as Dialog and treat it like Dialog also use DialogFragment
I want to display an alert dialog in my app. I am using fragments. I tried the below code to do this:
AlertDialog ad = new AlertDialog.Builder(context)
.create();
ad.setCancelable(false);
ad.setTitle(title);
ad.setMessage(message);
ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
but it was crashing and the error in logcat was:
04-18 15:23:01.770: E/AndroidRuntime(9424): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
From internet I came to know that the crash is due to context issue. I had given context as
context = this.getActivity().getApplicationContext();
I don't know what is the problem with this. Can anybody help me?
Replace context with getActivity().
The ApplicationContext should not be used for tasks such as creating Dialogs. As you are in a fragment you can instead get the Activity-Context simply by calling the Fragments getActivity() method.
More Information about this question (AlertDialog in a fragment, managed inside an event):
If you call AlertDialog within an event like onClick(View v) or onLongClick(View v) you can use
public boolean onClick(View v) {
...
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(v.getContext());
...
}
Try to use DialogFragment, DialogFragment is better when you use Fragments
I have had similar issues whereby I was trying to create an AlertDialog from a Fragment. A NullPointerException arose from it. Initially I did as follows:
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
The NullPointerException occurred specifically when calling alertDialog.show() later on in the code.
But after searching the documentation for AlertDialog.Builder(), there seemed to be another way to initialize it [AlertDialog.Builder Doc], which is to include a theme/resId as shown below:
AlertDialog alertDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_AppCompat_Dialog_Alert).create();
This resolved the NullPointerException at hand. Hope this helps you as well!
I used it in an adapter inside a listView, therefore I couldn't use getActivity(). In order to make it work I used getActivity() for the context in the instantiation of the adapter in the fragment:
this.adapter = new myAdapter(getActivity(), factory);
Later in the other class (the adapter's class) I was able to use getContext()and it worked.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog alert= null;
AlertDialog.Builder build= new AlertDialog.Builder(getActivity());
build.setTitle("title");
build.setItems(stringarrayname, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Toast.makeText(getActivity(), "hi", Toast.LENGTH_SHORT).show();
}
});
build.create().show();
You can try this or use DialogFragment
private void showAlert(final int position) {
new AlertDialog.Builder(getActivity().getApplicationContext())
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// deleteSuggestions(position);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
The solution is to replace by getActivity()
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity(),R.style.MaDialog);