Show a float dialog in any screen? - android

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

Related

What are differences betwen AlertDialog and AlertDialog.Builder?

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.

How do I pass arguments to a Dialog box in Android?

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

Dialog.getCancelable() doesn't exists

Is there any way to know if a Dialog was set to cancelable true or false?
ex : Dialog.setCancelable(true)
How do I get its value ?
No, the Dialog class does not have such feature in its API.
If you really need to, you can access the cancelable flag with Java reflection:
Field f = Dialog.class.getDeclaredField("mCancelable");
f.setAccessible(true);
boolean cancelable = f.getBoolean(yourDialog);
Since this is accessing Dialog class internals, there's no guarantee it will work on different versions of Android.
Of course, if you are creating the Dialog yourself, you know what parameters you have passed to it and can deduce cancelability from there.

Difference between AlertDialog.builder's obj.create() vs obj.show() vs obj.create().show()

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.

Get AlertDialog view of WebView

I'm creating a scenario test framework for Android Cordova/PhoneGap applications that builds on JUnit. There is one scenario I would like to test: you click on some button in the webpage (shown in an Android WebView) and it opens a popup (an AlertDialog). I want to check the message on this popup and click one of the buttons. Therefore I need the view object of this AlertDialog.
I know you can use findViewById, but you have to give an id as parameter, which I don't have since the dialog is created with the following code:
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx.getContext());
dlg.setMessage(message);
dlg.setTitle(title);
....
dlg.create();
dlg.show();
Any idea how I can access the correct view?
Thanks!
The TextView containing the AlertDialog's message is always identified by android.R.id.message. If you capture the result of dlg.create(), you should be able to get a reference to the message TextView by calling .findViewById(android.R.id.message) on it, from which you could then get the text.

Categories

Resources