Android - adding Views to the screen but outside of the content.

I am creating a component that needs to appear above and separate from the existing UI, for example, slide to the bottom of the screen and stay there, regardless of the current content. I would like it to be modular and portable, so the assumption that FrameLayout is not realistic.

When looking at the source for the Dialog and PopupWindow classes, it looks like they are doing this using the Window and WindowManager classes, but I cannot find much documentation on these classes.

Is there an easy way to accomplish what I am describing?

TYIA

+3


source to share


2 answers


In the interest of future searchers: The answer is to use the WindowManager class. It's pretty straightforward, despite the lack of documentation (which I can find, anyway):



WindowManager.LayoutParams wlp = new WindowManager.LayoutParams();
// assign position, dimensions, and layout behavior as properties of wlp
WindowManager wm = (WindowManager) getSystemService("window");
wm.addView(someView, wlp);

      

+1


source


The easiest way to do this is with RelativeLayout. Place the view where you want and then set the visibility to delete. When you want to show the View, start the animation and set the visibility to visible.

A Layout like this

<RelativeLayout ...>
    <LinearLayout ...>
    <!-- your main UI -->
    </LinearLayout>

    <LinearLayout 
        android:id="@+id/hiddenView"
        android:visibility="gone">
        <!-- the UI for the separate component -->
    </LinearLayout>
</RelativeLayout>

      



In code

Animation someAnimation = AnimationUtils.loadAnimation(this, R.anim.some_animation);
hiddenView.startAnimation(someAnimation);
hiddenView.setVisibility(View.VISIBLE);

      

The above is easy, but not very versatile, to make something a more robust means of writing a custom View or ViewGroup, and in-depth tutorials on such things are unfortunately a bit and far apart.

0


source







All Articles