Question

In my application I add a RelativeLayout to the window (they're a banner sort). Occasionally there could be more than one "banner", and so I want to count the number of views a.k.a "banners" inside the window, so I can set some MarginTop or sort to not let them overlap each other. Any idea?

private void initWindow() {
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mainNotificationFrameContainer = new LinearLayout(this);
    mainNotificationFrame = new RelativeLayout(this);
    notificationDisplay = new TextView(this);
    notificationIcon = new ImageView(this);
    closeBannerButton = new ImageView(this);
    fadeInNotificationBanner = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in_notification_banner);
    fadeOutNotificationBanner = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out_notification_banner);

    setUpViews();

    final WindowManager.LayoutParams mainNotificationFrameContainerLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );

    mainNotificationFrameContainerLayoutParams.gravity = Gravity.TOP;
    mainNotificationFrameContainerLayoutParams.y = 0;

    final WindowManager.LayoutParams mainNotificationFrameLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            150,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );

    final WindowManager.LayoutParams notificationIconLayoutParams = new WindowManager.LayoutParams(
            150,
            150,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );

    final RelativeLayout.LayoutParams closeNotificationBannerLayoutParams = new RelativeLayout.LayoutParams(
            150,
            150
    );

    closeNotificationBannerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    final RelativeLayout.LayoutParams notificationDisplayLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
    );

    notificationDisplayLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.notificationId);

    windowManager.addView(mainNotificationFrameContainer, mainNotificationFrameContainerLayoutParams);
    mainNotificationFrameContainer.addView(mainNotificationFrame, mainNotificationFrameLayoutParams);
    mainNotificationFrame.startAnimation(fadeInNotificationBanner);
    mainNotificationFrame.addView(notificationIcon, notificationIconLayoutParams);
    mainNotificationFrame.addView(notificationDisplay, notificationDisplayLayoutParams);
    mainNotificationFrame.addView(closeBannerButton, closeNotificationBannerLayoutParams);

    setUpListeners();
}

above is the function I used to add all the needed views

Was it helpful?

Solution

Simple solution:

Add one global LinearLayout and add the banners inside it, the LinearLayout has a method getChildCount().

Extra pros: You don't need to do a "MarginTop" yourself!

I use this method for my app Tinycore on the Play Store

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top