AlertDialog and SherlockActivity - android

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.

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

Chronometer inside AlertDialog

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.

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

AlertDialog setTitle() and setMessage equivalents in FragmentDialog

I'm rewriting an existing app for Honeycomb and I've run into a problem. In the existing app, we create the an AlertDialog with default title and message values, then replace them later if needed. To replace them, we use setTitle() and setMessage():
AlertDialog dialog = getDialog();
if (some condition) {
dialog.setTitle(R.string.error1);
dialog.setMessage(getResources().getString(R.string.error1_msg));
}
else {
dialog.setTitle(R.string.error2);
dialog.setMessage(getResources().getString(R.string.error2_msg));
}
However, now that we are using DialogFragment, there is no method for setTitle() or setMessage(), so we can't change it after it has been created. Is there a workaround for this case, or are we out of luck?
You have to extend the DialogFragment to provide content. See the documentation for examples and other options.

what context should i use AlertDialog.Builder in?

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

Categories

Resources