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());
Related
Just want to know if it is possible to make a chronometer appear inside of a AlertDialog?
AlertDialog.Builder popup = new AlertDialog.Builder(ScoreNewGame.this);
popup.setTitle("Timer");
What next though?
Kind regards
You can use any View (including Chronometer) in a dialog with the AlertDialog.Builder.setView() method.
how can I add a new dialog with new layout on the currently running activity without making changes to the layout file of currently running activity. I cannot add a new activity as the old activity should remain active while the new dialog is displayed.
Here is a small snippet to get started on creating a Dialog and applying a layout file to it.
// create an instance of Dialog
Dialog dialog= new Dialog(c, R.style.CustDialog);
//inflate a layout
LayoutInflater inflater = this.getLayoutInflater();
View root = inflater.inflate(R.layout.custom_alert, null);
// set the layout for the Dialog
dialog.setContentView(root);
If you read the Dialog Docs it gives the different methods you can use.
Note the docs say
The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses:
So for this reason you may want to look at AlertDialog which you can find plenty of examples of on SO or the Google.
This answer gives an example of creating a custom Dialog class.
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.
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();
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();