I can't parse the following lines of android(/java) code - android

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.

Related

android app crach when clicking alertdialog.show two times

Slam .
I am trying to show view inside alertDialog, every thing goes fine and the view popup without any problem, but when i click show view again the application crash and bring this error
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Slam again
the error was in showing the same view again,
i solve it by creating new alertDian everytime i click show view
customerDetails = new AlertDialog.Builder(OrderDetails.this);
LayoutInflater inflater = getLayoutInflater();
convertView = (View) inflater.inflate(R.layout.customer_details, null);
You're reusing the same view for the alert dialog. Either don't do that (create a new one), or you must remove it from the previous dialog. Making a new one is likely easier.
The problem comes up when opening the dialog for the second time, right? You're trying to re-use the same view with the alert dialog which is why you're seeing this error.
You can either completely remove the view before re-inflating the dialog, by calling removeView() as written in the error message. Or just inflate the view again, which is what is normally done. You want the view to live and die with the dialog. To do this, you should inflater.inflate(...) the view alongside your dialog creation code.
If you post your code, we can help you further.
I solved this issue by creating the separate layout xml file with the Edit Text. After that I did :
//alert_edit_text is the edit text id which is in separate file
val builder = AlertDialog.Builder(this)
val inflater = layoutInflater
val dialogLayout = inflater.inflate(R.layout.alert_edit_text, null)
val editText = dialogLayout.findViewById<EditText>(R.id.edit_text_alert)
After your certain code,
setView(dialogLayout)
show()

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

Null Pointer error when trying to utilize a class with AlertDialog.Builder [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Trying to inflate an AlertDialog.Builder. I'm trying to get the date wheel (Yuri Kanivets' wheel) to appear inside of my dialog. Since the exact code that I need exists inside one of his classes, I'm just trying to instantiate a new instance of his DateActivity class (which I've imported into my project), and then add that to my dialog. Unfortunately, I can't seem to connect my DateActivity object with my dialog. I thought that it would be one of the arguments where I inflate the view, but that crashes. Here's my code:
EDIT: To clarify, in the code that follows there are no errors. The problem as I mentioned is that there is no usage, and therefore no connection, of my DateActivity variable with the AlertDialog.Builder. I've tried using that variable (dateWheelSelector) as an argument to builderView and also to the builder variable instantiations, but both of these crash. I need to figure out how to connect these since right now my dialog is empty.
private void setStartDate() {
//somehow I need to use this variable, but where???
DateActivity dateWheelSelector = new DateActivity();
LayoutInflater inflater = LayoutInflater.from(this);
View builderView = inflater.inflate(R.layout.wheel_date_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(builderView);
alert = builder.create();
/* Set the title of this dialog programatically */
TextView title = (TextView) builderView.findViewById(R.id.date_title);
title.setText("Choose Start Date");
alert.show();
}
Thanks for any suggestions.
You cannot add an Activity to a dialog. You can either define the Activity to be a Dialog (see Android Activity as a dialog) or you can refactor your DateActivity to be a DialogFragment (see http://developer.android.com/reference/android/app/DialogFragment.html) that can be used as Fragment or as Dialog.

Does Android API have something like Flash's duplicate movieclip?

I used to do alot of Flash Actionscript and now I am getting into Android. Is there something in the Android API that is similar to duplicateMovieClip() in Actionscript? I'm sure there is probably a way to write such a method, but I am wondering if there are any existing shortcuts.
For example, say I have an ImageView, TextView, or other kind of View Object on screen and I want to have a button to click which will make a duplicate of some object on screen.
If you don't mind my asking, why do you need something like duplicateMovieClip()?
To answer the question, Android doesn't have a notion of the AS2 duplicateMovieClip(). Much like in AS3 (which also didn't have duplicateMovieClip()) you'll have to implement your own cloning method. Java does have an unimplemented '.clone()' method as part of every Java object, so if there's a particular View you would like to clone you might be able to implement your cloning there by
Overriding the clone method.
I think what you'd probably end up doing instead is doing something more akin to instantiating from the Library by making small view layouts in xml and inflating them using the Inflater tools.
View result = null;
// where pContext is a context object, either supplied by the application
// or just by the current Activity (if available)
LayoutInflater inflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// where id is the layout id such as R.layout.myclonableview.
// where pRoot is the parent container for the new result.
// where pAttachToRoot is whether to immediately inflate the new view into the root.
result = inflater.inflate(id, pRoot, pAttachToRoot);
// Now "clone" your old view by copying relevant fields from the old one to the
// one stored in result

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