what context should i use AlertDialog.Builder in? - android

Could anyone please explain what context should i use the AlertDialog.Builder class? I am new to android app development and I frankly don't understand which context to use when?
Say, I want to create an object for AlertDialog.Builder class -
AlertDialog.Builder ab = new AlertDialog.Builder();
ab.setMessage("Test");
ab.show();
What context should I use it in? Does it differ if I use the Alert Dialog onCreate or OnClickListener or in the handler of any such event?

You should use the context of the Activity that it's executed from. In other words, just use YourNameOfActivity.this as context.

In the first version of my app I made the mistake of not using onCreateDialog and instead built and showed the dialogs myself. If you do it yourself you have to take care of things like dismissing the dialog before the activity is finish()ed otherwise a window will leak.
I would override onCreateDialog in your activity and return ab.create() (not show()). onCreateDialog will then handle showing the dialog and you'll just have to call showDialog(id).

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Test")
.show;
(or)
if u want (yes,no) button means use this
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener)
.show();

Related

How to Keep showing the dialog when the Activity is change

I used a custom dialog as a notify in the app, so I want it to keep show even the activity is changing, can it? or I should use other thing?
Use getApplicationContext().
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

Understanding the AlertDialog creation on android

I'm new to Android, only three days mostly studying the basics. I ended up with this code while studying the creation of an android alert dialog:
OnClickListener oclBtnOk = new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
}
});
alertDialog.show();
}
};
But I still have somethings to get known:
1- First time I made the code I was using AlertDialog alertDialog = new AlertDialog.Builder(this).create(); instead AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();. The first code wans't compiling, so, what exactly is the difference? That means the dialog builder is son or has some dependency with the MaiActivity? That means the listener from this dialog is inside the main activity?
2- Is this really the correct way to create a simple alert dialog? 'cause in my console I saw one red line saying "ActivityManager: Warning: Activity not started, its current task has been brought to the front" and also studying through http://developer.android.com/guide/topics/ui/dialogs.html what they recommend is to use AlertDialogFragment as container of my dialog..
And my last question is a little bit more complicated but a simple answer as "Yes" to lead me I a deeper research would help- android has some sort of EDT (Event Dispatch Thread) since is based on java? I mean, to process graphics (like progress bars) should I separate them into another thread?
AlertDialog.Builder requires a Context. The code is in an OnClickListener anonymous subclass and it is not a Context. this refers to the subclass instance. To refer to the this of the parent activity class, it's scoped as MainActivity.this, and activities are Contexts.
It's all right at this point of your learning curve. The "Warning: Activity not started, its current task has been brought to the front" is nothing to worry about - the app was already running and was just brought to front, not re-launched.
Android does not run AWT and there's no EDT by that name. However, the main thread (also called the UI thread) does something similar.
To answer your first question:
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
The code is running in an anonymous class, so this does not reference an object derived from Context. Adding the MainActivity. makes the argument reference an Activity which is derived from Context
Your second question: Is this really the correct way....?
It is certainly one acceptable way. There are others.
The message you are seeing about bringing the activity to the front is not related to dialogs. You may eliminate it by exiting from your application before starting a new debug session.

AlertDialog and SherlockActivity

Im struggling to get a AlertDialog working whilst in an onClick method of a Sherlock activity. Here is my code.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Ooops!!");
builder.setMessage("Sorry.");
builder.setPositiveButton("OK", null);
AlertDialog dialog =builder.create();
dialog.show();
Its new AlertDialog.Builder(this) where the problem is. This is underscored as an error, I also tried getActivity() which is common in Sherlock but no luck. Any ideas or direction?
Replace this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
By
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this);
I guess you use AlertDialog.Builder(this) in onClick of your activity in which case it does not refer to the activity context. So use ActivityName.this where ActivityName is the name of your Activity.
If this code is inside an Activity, then use [activity-name].this to reference that Activity.
If you are using ActionBarSherlock, you will most likely always use "getSherlockActivity()" in place of "getActivity()"
We use ABS in almost all of our apps for legacy support. Let me know if it works.

Display Android dialog on top of another?

I have 2 alert dialogs, dialog A and dialog B. Clicking on one of dialog A's buttons will bring up dialog B. I then want to have a button that will dismiss dialog B and return to dialog A.
Is there a way to do this apart from dialog B performing a showDialog(dialogA) ?
This works, but you can see the reload of dialog A, instead of just returning to an already existing dialog A. Performing a dismiss in dialog B just dismisses both of them.
A minor question, but I'd like to see if there is a way to stack them on top of one another.
Thanks
Using the basic dialog building blocks it is not possible to have them stack, you will need to re-show the first dialog.
The reason for this is that when you press a dialog button it internally will dismiss the dialog as part of the process for calling the click handler you assigned for each button in the dialog builder API.
One way around this is to make a custom dialog layout that doesn't have the dismiss behavior, by setting up your own buttons in the layout, rather than using those created by the dialog builder methods. Then in the click handler for you own buttons simply show the second dialog without dismissing the first.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
As one reply mentioned, you cannot do this with standard dialogs. But you can do it by making the first dialog be an activity that is styled to look like a dialog, and the second is actually a dialog.
Just set the theme of the activity in your layout like so:
<activity android:theme="#android:style/Theme.Dialog">
See this topic on making an activity that looks like a dialog.
https://stackoverflow.com/a/1979631/602661
dismiss the dialog from within itself.
Edit, here is some clearer code.
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
});
alertDialog.show();
You should use inside your custom layout one view/button and based on this view/button click you can create another dailog without cancel first one, if you use builder.setNegativeButton or builder.setPositiveButton your current dialog will be close, my working code like as,
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityAppImages.this,R.style.your_style);
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.your_custom_layout, null);
final Button mButtonCreateOtherDailog = (Button)dialoglayout.findViewById(R.id.txt_create_second_dailog);
mTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//create your other dailog here
}
});
builder.setView(dialoglayout);
builder.show();

Enter data in an Android popup

I want the user to enter a string into an EditText into a popup. I have taken a look in Android Developers here .
But this kind of popup is not explained. How can I make it?
On the same page you linked to, check out how they say to create a custom dialog.
Create an XML layout for the dialog that has an EditText. Then show it:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();
Create your own custom dialog by extending Dialog. Your custom dialog class will have an onCreate() callback, in which you can call setContentView to whatever view you want, just like with an Activity.
Simply create a view to look however you'd like and use that one. Then, when you want to use your dialog, simply get an instance, like Dialog myDialog = new MyCustomDialog(getParent(), R.style.some_style); and then myDialog.show();

Categories

Resources