0

so I have previously used to create Activities as a "User control" when writing Android apps, but then I wanted to move over to "custom components" or "compound components" since I hoped that would give me a "self-containing unit"; a class that can be instanciated so that I can access my methods etc.

In short, something that could be used in the same way as a "User Control" in .NET, ie a class that defines UI components as well as has code to handle user input and other logic.

However, how do I add that Component to the current Activity using code, and dynamically show it as a floating popup?

Note that I do not want to add a Component design-time in an XML-file, but I want to create it "dynamically" (layout in XML of course, but not to add the Component to another layout)

Let's say I have a list with items, and when someone clicks on a list item, a new Component should be created and shown as a "floating popup" over the main activity.

If I would have done it using Activites, it would look something like this:

Intent i = new Intent(getApplicationContext(), MyActiviy.class);
startActivity(i);

which would have shown the Activity "on top" according the the layout. But in this case I have no object to interact with (myActivity.doSomeCode() for example), which is what I was hoping to have with a Component.

So, the question is: How do I create and add a custom Component, so that it is shown in the same manner as with a Activity and so that I have access to the instance (the object) of the class?

I hope Im not too fuzzy =)

Ted
  • 19,727
  • 35
  • 96
  • 154
  • you mean this? http://stackoverflow.com/questions/5944987/popupwindow-in-android – Sergey Benner Feb 19 '12 at 00:21
  • Hmm, thx for the info, but I dont think that'll match exactly what I want. In the example, they never create an actual object of the Component-class, which is what I want (so I can pass in data to it for example). Something like: *"new PopupWindow(new MyComponent());"* would have been nice. Another issue is that the constr for the Component requires Contex and AttributeSet, and I dont know where AttributSet comes from (and I cannot have NULL as it gives a NullPointerException). – Ted Feb 19 '12 at 00:32
  • you can have a dialog instead and just Dialog d = new Dialog(this);.... or create your own dialog class and do whatever you want with it. I don't think you can get rid of the context because usually UI components are bound to a calling parent activity and so on.... – Sergey Benner Feb 19 '12 at 00:35
  • The context I dont wanna get rid of, but I want to get rid of the AttributeSet. =) Hmm, I was hoping that these Components could be used somehow... I feel I am doing something wrong... – Ted Feb 19 '12 at 00:37
  • you can create a dialog/popup on ListView itemclick what's wrong with that? and set anything you want to that component – Sergey Benner Feb 19 '12 at 00:40
  • The problem is that I need to do something like this: *myComponent.setObject(MyObject o)*. Then the Component should act on the MyObject in some way (update GUI, do stuff etc). Sometimes its not supposed to be a popup, but placed "inside other layout". Again, I am referring to User Controls in .NET. The UserControl kan be placed "anywhere" (in a popup form, on a another UserControl etc) and is "self contained" (all code is in the control/class)... – Ted Feb 19 '12 at 00:44
  • you can pass this object as a constructor parameter. can't you? – Sergey Benner Feb 19 '12 at 00:46
  • Well, I don't see how. Im having problems with the constructor, as it expects to have (Context c, AttributeSet as) as parameters. Somehow I want the Component to be a View, with the containing code. Im not sure I'm being very clear in what I want (even though I know exactly what I want). Basically, can I re-create the function and workings of a .NET "User Control"? =) – Ted Feb 19 '12 at 00:51
  • I really don't see where the problem is. You extend a Dialog for example http://developer.android.com/reference/android/app/Dialog.html create a new constructor for it and go ahead do your thing. – Sergey Benner Feb 19 '12 at 00:58
  • Thanks for the ansers Sergey =) Well, the problem is that I cannot extend a Dialog as I sometimes need to place the "user control" on the "surface" and not as a Dialog/popup. I want to separate if its in a popup or if its somewhere else, not to force it either way. – Ted Feb 19 '12 at 01:36
  • Uhmmm perhaps this? http://developer.android.com/reference/android/view/ViewStub.html – Sergey Benner Feb 19 '12 at 01:37
  • And if I try the approach of MyComponent extends RelativeLayout and then try to show it using PopupWindow, the size of the PopupWindow has to be specificed manually, which is also a problem. The size should be whatever size the layout is... And that works fine if I use the Activity approach. – Ted Feb 19 '12 at 01:38
  • Its not a question of just inflating an XML/layout. I also need the code to be "encapsulated" by the class (thus mimicing a User Control in .NET). The code and layout should be packaged as one, so that I can access my methods and also place the whole thing in a Dialog, in other containers (just as a UserControl) – Ted Feb 19 '12 at 01:39

0 Answers0