0

I'm trying to make my own basic 'help overlay'. I have achieved this much so far Take a look

this was done by calling the addContentView with an instance of a custom view passed in. However i will only be able to hide the custom view instead of completely removing it after I am done with it.

It is very basic so far so there is no flair or pizazz yet. I am trying to further improve its ease of use. https://stackoverflow.com/a/10217050/3194316 this stack overflow answer suggests setting the content view to a dynamically created Framelayout and inflating the views. I have added the code

FrameLayout layout = new FrameLayout((Context) activity);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);

View parentView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
((ViewGroup) parentView.getParent()).removeAllViews();
activity.setContentView(layout);

layout.addView(parentView);
layout.addView(this);

where this is an instance of the custom view that sets the overlay shown below. activity is an instance of the activity in which this is presented in. I was at first getting an error that the parentView already had a parent, so this is why View parentView = activity.getWindow().getDecorView().findViewById(android.R.id.content); ((ViewGroup) parentView.getParent()).removeAllViews(); was added. I am now unforunately receiving a stack overflow error, and I cannot seem to understand why. Is there a better approach to this situation?

SOLUTION

Wrapping everything in a popup window allows the entire overlay to be dismissed properly by the android System.

FrameLayout layout = new FrameLayout(context.get());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);

LayoutInflater inflater = (LayoutInflater) context.get().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);

View overlayBase = inflater.inflate(R.layout.help_frame, null, false);
layout.addView(this);
layout.addView(overlayBase);

popupWindow = new PopupWindow(context.get());
popupWindow.setContentView(layout);
popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.showAtLocation(this, Gravity.CENTER, 0, 0);

context was a class level WeakReference variable for the custom view, instantiated when the constructor was called

In case anyone is interested, here is the code I used to create the popupwindow

Here is the (not final) result Take a look

Community
  • 1
  • 1
rperryng
  • 3,233
  • 3
  • 22
  • 35
  • I didn't get what exactly you are trying to achieve but you're doing it wrong. Have you considered PopupWindow? – Yaroslav Mytkalyk Feb 19 '14 at 15:51
  • Basically highlight a certain element on the screen [See here](http://acko.net/files/android/app-drawer.png). To be honest I wasn't aware of pop up window. Looks interesting. – rperryng Feb 19 '14 at 15:55
  • on the screenshot above it's just a fullscreen ImageView overlay. Anyway, take a look at the PopupWindow. As far as I understand you need to show some popup anchored to a View. That's what it does. – Yaroslav Mytkalyk Feb 19 '14 at 15:58
  • It is not. [Take a look](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.0.1_r1/com/android/launcher2/Cling.java). The screenshot doesn't show it but there is also a button in the bottom hand corner to dismiss it. Thanks for the popup window suggestion. I am going to play around with it and will come back with results – rperryng Feb 19 '14 at 16:01
  • a [library extending this code was created as well](https://github.com/amlcurran/ShowcaseView) – rperryng Feb 19 '14 at 16:02
  • This was a good call. Would you like to duplicate your comment in the form of an answer so I may accept it? @DoctororDrive – rperryng Feb 19 '14 at 21:41

1 Answers1

0

If you want a dynamically appearing View anchored to another View you should use a PopupWindow

Is there a simple example of the PopupWindow class using Android v2.0?

Community
  • 1
  • 1
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99