6

Already asked a similar question, yet without much luck.

Suppose I have a service and I need a view to pop up above it. In the same time, they both should be intractable, i.e. the user should be able to both click buttons within the view, as well as ones on the service in the background.

enter image description here

Is this in theory possible? If yes, how should I initialize that view?

Thanks!

Roger
  • 6,443
  • 20
  • 61
  • 88
  • 1
    I don't understand: show a view above service? Services are generally invisible in android (do not not have any UI). Maybe you need some kind of customizable Toast functionality. Try to edit your post and provide more details... – pkk Sep 22 '11 at 20:53

6 Answers6

5

Yes it's possible, what you need to do is call the WindowManager service and add your view via the same.

WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout layout=(RelativeLayout) inflater.inflate(R.layout.box,null);

You need a WindowManager.LayoutParams object which should contain the parameters for the layout

windowManager.addView(layout,params);

Well, adds the view

brindy
  • 4,585
  • 2
  • 24
  • 27
000
  • 51
  • 1
  • 2
4

What you want is to add a view from your running service instance. This way you can persist the view across all activities - and from anywhere else. See this great example:

http://www.piwai.info/chatheads-basics/

slott
  • 3,266
  • 1
  • 35
  • 30
3

Services most definitely can have a user interface: Input methods are an obvious example. See http://developer.android.com/resources/samples/SoftKeyboard/index.html for an example.

Aharon Manne
  • 712
  • 3
  • 11
  • 34
2

I guess you are misusing the word "Service". Service is invisible, Activities are visible.

There are no buttons in an Service!

So you have no choice! You should put both views in one Activity, and I would use a RelativeLayout and set the visibility of your chidren to GONE/Visible.

http://developer.android.com/reference/android/widget/RelativeLayout.html

Also using a popup and making the layout under it clickable will disturb the user. You are completely changing User experience. I strongly suggest too make your popup appear at the top/bottom of your initial layout

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • 1
    I think this one is your best bet if you absolutely must achieve this effect. Make yourself a Layout contained inside your Activities main layout that sort of looks like a pop up and just make it visible when you want it to be. Other than this there is no way to have both the foreground and the background buttons be clickable. – FoamyGuy Sep 22 '11 at 23:44
1

Services run in the background and do not have an UI. So you can not show something over a Service.

If you need a Service to notify user about something, then use Notification.

Ayou could use a Toast, but I advise against it, as it can confuse users since it can pop-out over another app's activity.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
1

What you want is an Activity instead of a Service and a Dialog instead View. I suggest you read this document by google: http://developer.android.com/guide/topics/fundamentals.html

However to answer your question about both being interactable. This isn't possible. At any given time 1 and only 1 activity is on the top of the activity stack. The user can only interact with that activity. If you want something like a floating window then will have to create it yourself. Although keep in mind that goes against the android design principles.

slayton
  • 20,123
  • 10
  • 60
  • 89