Which one is ideal for creating alert dialog? what are differences
AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());
or
AlertDialog builder = new AlertDialog.Builder(this.getContext()).create();
Basically AlertDialog is implemented by Design Builder Pattern.
Design Builder Pattern provides flexibility of adding dynamic API methods at any class rather that changing any existing code from it. Only disadvantage is it should not directly create object of that class. It should always have Builder class prepared for that.
So, if you try creating object of AlertDialog something like new AlertDialog(), you won't be able to & here's where Builder pattern comes to picture.
Refer here for more details.
Related
I am creating AlertDialog.Builder to display user whether data is available or not, entry is deleted or not etc. I am confused about creating object in onCreate() method or creating local object of AlertDialog.Builder in function. Which one is optimal?
I guess creating a single object in onCreate() because it located once memory from heap and it required throughout Activity . Am I correct or not?
//This Alert Dialog use for various button .That's why we building single Object only
AlertDialog.Builder buidler = new AlertDialog.Builder(MainActivity.this);
If you will reuse the instance of AlertDialog.Builder you should create it one time in onCreate() and re-use it when needed. Otherwise, create it when needed.
Google decided to make a single-threaded user interface that doesn't have modal dialogs. I'm sure most of you have found that nothing updates until your function returns because everything is event driven on a single thread (by "law").
If I have a simple alert-box, such as "Are You Sure?" (example only), with a Yes and No button, then I have to assign callbacks to the buttons rather than having a simple return value (no modal dialogs). That's fine, even though a return value would vastly simplify my problem (arguments stay local to the caller), although this would stop the calling activity from responding (modal).
Imagine now if I have a list of items and the user attempts to perform some operation. The dialog must now have some way to pass WHICH item I want to perform the operation on to the button's callback, but I can't seem to find any mechanism in the API for passing this along to the onclick handler. Using non-local variables is a work-around, but messy.
How can I pass this information along cleanly? Does anyone have some sort of hack that would somehow "fake" a modal dialog that can return a value (I'm not seeing how).
Create a custom dialog that extends the default android Dialog and add the information you need and pass on the constructor.
See more here: How can I pass values between a Dialog and an Activity?
I am not sure what exactly what do you want to achieve. Not sure if your problems is in the communication between the activity to the dialog or dialog to the activity or both.
Anyway, I have some experience on Android and I really recommend you to achieve the communication between activities, fragments, even dialog (DialogFragments) to use one of these libraries. At the beggining could be a little bit hard to understand how work, but the result is faster and cleaner code, of course offers you more flexibility.
Take a look to:
https://github.com/beworker/tinybus --> less used but it is awesome
https://github.com/greenrobot/EventBus --> more extended and used for the community
Hope to help you!
In a situation like this, I Created a new string array entry in the strings.xml in values folder like this:
<string-array name="array">
<item>1</item>
<item>2</item>
</string-array>
And then create a dialog using Dialog builder like this:
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
LayoutInflater infl=this.getLayoutInflater();
Resources res=getResources();
dialog.setSingleChoiceItems(R.array.alphabets, 0,new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mReturnVariable=which;
}
});
dialog.create().show();
So the mReturnVariable contains the user selected item index .Hope that solves the problem
I passed the required arguments to the Alert Dialog using View Binding in Android Latest version.
private ConnectDialogBinding connectDialogBinding;
private String chargerID;
private void connectDialog() {
// Create the object of
// AlertDialog Builder class
AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);
connectDialogBinding = ConnectDialogBinding.inflate(getLayoutInflater());
builder.setView(connectDialogBinding.getRoot());
connectDialogBinding.txtID.setText(chargerID);
builder.setCancelable(false);
// Create the Alert dialog
AlertDialog alertDialog = builder.create();
// Show the Alert Dialog box
alertDialog.show();
connectDialogBinding.cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.cancel();
}
});
}
enter image description here
I want to show a dialog in my android phone anywhere. I want to use windowmanager.addView()
to come true it. But It doesn't work because this function only can add views.How to show a dialog in anywhere?
Refer this links http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW
Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. Very few applications should use this permission; these windows are intended for system-level interaction with the user.
Constant Value: "android.permission.SYSTEM_ALERT_WINDOW"
Example projects
https://github.com/fouady/SpotifyTray-Android , https://github.com/henrychuangtw/FB-ChatHead
Use code like this (it's from android documentation):
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
I'm working my way threw a couple of books on android programing with a friend of mine (So I guess you'd kind of call this homework?) We have come across code segments like the following example a number of times and I have never fully understood the segments, just mindlessly used it. Would someone have the time to break the example into a couple of lines NOT held together with the "dot" operator? I've tried a bunch of times and I'm pulling my hair out over it and I'm old enough that I don't have much hair left.
View v = getActivity().getLayoutInflater()
.inflate(R.layout.some_dialog, null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.some_dumb_title)
.setPositiveButton(android.R.string.ok, null) // null can be On Click Listener
.create();
Maybe a pointer to some web pages that uses simple lines of code to do the work. I know the way it is written is more efficent but seeing it laid out as seperate lines would make it much easier to understand and more important, easier to debug.
Looks like it's using the builder pattern to create an AlertDialog. If we follow what the documentation says about it, we arrive at the following:
View v = getActivity().getLayoutInflater()
.inflate(R.layout.some_dialog, null);
This roughly translates to:
Get an activity on the current instance or super class
With that activity, get its layout inflater
With that layout inflater, inflate it with the arguments R.layout.some_dialog, null.
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.some_dumb_title)
.setPositiveButton(android.R.string.ok, null) // null can be On Click Listener
.create();
This roughly translates to:
Get a new builder, passing along the activity of the current instance or super class
Then, with that builder, set the view
With the same builder, set the title to R.string.some_dumb_title
With the same builder, set a positive button with the parameters android.R.string.ok, null
With the same builder, invoke create, thus creating the AlertDialog.
Activity a = getActivity();
LayoutInflater inflater = a.getLayoutInflater();
View v = inflater.inflate(R.layout.some_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v);
builder.setTitle(R.string.some_dumb_title);
builder.setPositiveButton(android.R.string.ok, null); // null can be On Click Listener
AlertDialog dialog = builder.create();
return dialog;
Both of these examples chain methods together since we don't care about the references in between, we care about the final product. If your code works well, and you know what you're doing, chaining is convenient. For debugging purposes, don't chain methods, the stack trace will not be as helpful (consider what happens is getLayoutInflater() returned null when chaining methods.
It is worth noting though, that for the AlertDialog.Builder example, each of its methods returns the same AlertDialog.Builder Object, whereas in the first example, you are getting a lot of different Objects in an attempt to inflate your given View.
Is there any difference between .create() and .show() methods of AlertDialog's builder class? Like when we create an alert dialog using:
AlertDialog.Builder builder = new
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("");
builder.setPositiveButton(....)
builder.setNegativeButton(....)
What is recommended pratice to use and why?
builder.create() //I have seen this creates and displays the dialog
OR
builder.show() //this also displays the dialog
OR
builder.create().show() //well same thing
I have read the documentation. But was unable to make any sense from it. Any ideas ?
obj.create()-For create Dialog
obj.show()-For show Dialog <- without it you cant show dialog if you created.
and
obj.create().show()-create and show Dialog i mean both same as above two in one statement.
builder.show() returns an AlertDialog object and displays it immediately. As stated in the documentation, calling this method is functionally identical to:
AlertDialog dialog = builder.create();
dialog.show();
builder.create() returns an AlertDialog object with the arguments supplied to the builder, without showing it. This might be useful if you want to create and store the AlertDialog object for additional processing, after which dialog.show() may be called.