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()
Related
I am new in android.I have a simple doubt,Is there any way to get id of a widget from some layout without set it as content view ?? I am looking to invisible a view from a class .I used the code
View b = findViewById(R.id.id2);
b.setVisibility(View.GONE);
But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? please help me. Thanks in advance :)
You can inflate the views separately and then use findViewById on the root of the inflated layout.
// inside of Activity, you can use 'this' for the context
LayoutInflater inflater = LayoutInflater.from(context);
View root = inflater.inflate(R.layout.some_layout);
// from your example
View b = root.findViewById(R.id.id2);
b.setVisibility(View.GONE);
At this point however these views are not part of your Activity's view hierarchy and will not be seen by the user. You will have to add them either by using setContentView(root) or by finding a ViewGroup in the current view hierarchy and calling viewGroup.addView(root).
You can do it this way
Button filterButton = new Button(YourActivity.this);
filterButton.setVisibility(filterButton.GONE);
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(view) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
See below link :-
Why findViewById() is returning null if setcontentview() is not called?
'But error in "id",is it possible to get ids if widget from a java class without set as contentview ?? '
You have to create dynamic view then you do not have need to call setcontentview.
how to set setContentView?
Is there a way to set setContentView(int id) dynamically?
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.
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.
I'm new to Android and find it brutal (there seems to be an near infinite number of details and dependencies to remember).
Anywho, I got the TextSwitcher1 example app working, which uses ViewSwitcher. I'm assuming ViewSwitcher is the way to go, need to either display a map or a table, user can pick, and switch back and forth.
So I created my MapActivity in another application, seems to work. Next integrate into main app. So, call
View v = findViewById(R.layout.mapview);
and then
mSwitcher.addView(v);
except "v" is null. Why? Do I create the activity? But I don't want to show it yet. Is there such a call as "create activity but hide it until needed"? Or am I barking up the wrong tree?
Thanks for any insight.
The findViewById function returns a View based on an ID resource (R.id.something) for whatever view you have loaded in your activity (using setContentView(R.layout.main)). In your sample code, you're using a layout resource (R.layout.mapview). You should inflate the XML file, which will return a View that you can use to add to the ViewSwitcher.
Example Code:
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.mapview, null);
mSwitcher.addView(v);
However, you should be able to define everything in your XML file and not have to manually add the pages to your ViewSwitcher. Here's some example code on how to do that: http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/
In my application I have 2 layouts. One's is root layout that changes dynamically and the second is just a form that opens many times. The form must be child of the root layout but I'm failing to perform so.
I assume that I should simply use:
main.AddView(formLayout)
but I can't figure out how get this formLayout object.
Will thank you for possible answers.
Sounds like you need the LayoutInflater object Android reference.
This allows you to create an object from the xml layout in your project.
With the advice of cjk I wrote piece of code that actually answers my question:
setContentView(R.layout.main);
main = ((ViewGroup)findViewById(android.R.id.content));
LayoutInflater inflater = this.getLayoutInflater();
ViewGroup form= (ViewGroup) inflater.inflate(R.layout.formLayout, null);
main.addView(form);
Thank you all
Not sure if I understand the question properly, but something like that might work:
View myView;
myView = (View) this.findViewById(R.id.formLayout);
main.addView(myView);
By get I figure you mean you want to retrieve a field in the new opened layout, you can do it by making it a new Intent and using startActivityForResult instead of startActivity.