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.
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.
Is there any way that i can set an attribute globally and request focus whenever an error is set?
Suppose i am validating a lot of EditText fields and setting an Error for each of them, after setting each other i have to request a focus:
editText.setError("Error");
editText.requestFocus();
However there exist a lot of fields and i am required to do call .requestFocus() in every single one. Is there any way to set it globally or setting explicitly is the only way around?
you can create one function for that and call that function in your whole app by giving class reference.
public static void EdittextError(EditText editText, String Error) {
editText.setError(Error);
editText.requestFocus(); }
Pass edit text obj. and Error String in function parameter.
Is there any way to set it globally or setting explicitly is the only way around?
No, there's no special attribute in system widget, so you have to set it by hand or extend EditText and override setError() to automatically request focus when called and then use MyEditText in your layouts
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 want to show a dialog when the user clicks on an option menu inside an Activity. I first wanted to do this using the Dialog class. The code is something similar to the one below.
final Dialog d = new Dialog(this);
d.setContentView(R.layout.customDialog);
d.setTitle("Sample title");
data = (EditText) d.findViewById(R.id.data);
button = (Button) d.findViewById(R.id.aButton);
d.show();
button.setOnClickListner(new View.OnClickListner() {
// grab data from edittext and save it to some var
d.dismiss();
});
Something like that. The dev guide suggests I not instantiate directly a Dialog class. Is there something particularly bad about this approach ?
The android dev guide adds a lot of extra info to help developers avoid tasks which take up a lot of processing time. Instantiating a Dialog class directly probably takes up a lot more processing time.
The system keeps a cache of dialogs, so you don't have to manage it yourself. Each dialog is created only once and stored somewhere so it can be reused later. That's because creating a dialog is computationally expensive, I guess. The system gives you the hooks to create the dialog the first time it's needed and to prepare it just before showing