Android: WindowManager views count - android

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

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

Related

Issue with programmatically trying to set a FrameLayout for Android

I am able to successfully a achieve this doing so with the XML layout as seen in the image below:
Then when trying to do so within Java I am not able to get the same result as seen in the screen shot:
Here is the Java code I have that renders the result above:
FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);
ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);
TextView bodyTextView = new TextView(this.context);
bodyTextView.setBackgroundColor(Color.TRANSPARENT);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.BOTTOM);
frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);
Can anyone see what I am missing or doing wrong?
UPDATE:
Based on the response below the screen shot results in the following:
You need to add the layout gravity as a LayoutParams to the TextView:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
And change the gravity of the the TextView to the center:
bodyTextView.setGravity(Gravity.CENTER);
Here's the entire layout:
FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);
ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);
TextView bodyTextView = new TextView(this.context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
bodyTextView.setLayoutParams(params);
bodyTextView.setBackgroundColor(Color.GRAY);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.CENTER);
frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);
setContentView(frameLayout);
I change to kotlin sorry but, test this code
val frameLayout = FrameLayout(this)
val layoutparams = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL or Gravity.CENTER_HORIZONTAL
)
frameLayout.layoutParams = layoutparams
val bodyImageView = ImageView(this)
bodyImageView.setBackgroundResource(R.drawable.ic_launcher_background)
val params =
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
params.gravity = Gravity.BOTTOM
val bodyTextView = TextView(this)
bodyTextView.text = "SF"
bodyTextView.gravity = Gravity.CENTER
bodyTextView.setTextColor(Color.YELLOW)
bodyTextView.setBackgroundColor(R.color.purple_700)
frameLayout.addView(bodyImageView)
frameLayout.addView(bodyTextView,params)
You only have to add parameters to your view
in Java
// Add this code
FrameLayout.LayoutParams layoutparams =
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutparams.gravity = Gravity.BOTTOM;
// Add in your code for textView
frameLayout.addView(bodyTextView,layoutparams);

Fullscreen view with gravity.BOTTOM is unwantingly clipped when an overscan value is set

My Android (8.0+) app is implementing a narrow overlay along the bottom of the screen, but when a user sets an overscan value, it disappears. How can I make my view respect what is actually visible so Gravity.BOTTOM will not be clipped? I have tried countless combinations of flags, but nothing seems to ackknowledge the overscan value.
Point screen_size = new Point();
WindowManager.LayoutParams mParams;
WindowManager mWindowManager = getSystemService(WINDOW_SERVICE);
mWindowManager.getDefaultDisplay().getRealSize(screen_size);
layout = new LinearLayout(getApplicationContext());
layout.setOnTouchListener(this);
// color for debug.
layout.setBackgroundColor(0x55ff0000);
mParams = new WindowManager.LayoutParams(
screen_size.x,
40,
0,
0,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.BOTTOM;
mWindowManager.addView(layout, mParams);

Move popup window out of screen

Hello i have this code to show popup window...
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
(width/100)*50, height, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = (width/100)*50;
parameters.y = 0;
But i need to show it out of screen...
When i set x position to more then windows size it's stuck at corner and not go over screen...
Thanks.
Edit: Because some people don't know what i mean.. i mean half outside screen not full..
You need to add one more flag which is flag_layout_no_limits
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
(width/100)*50, height,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = (width/2);
parameters.y = 0;

How to add multiple views to window manager in Android

I am developing a application like Facebook Chat Heads a know how to add a single view to window manager.
How to add multiple views to window manager? I tried frame layout and relative layout, but how can I move chat head from one place to another place if I am using relative layout?
For adding multiple views I used below code:
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.ic_launcher);
TextView t = new TextView(this);
t.setText("Blessan Mathew");
t.setBackgroundColor(Color.CYAN);
params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.addView(t, params1);
params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.addView(chatHead, params1);
fr.addView(childLayout);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(fr, params);
How can I drag chat head to remove its view?
To add notification on top of ImageView and move both the views together:
Use RelativeLayout as parent Layout and add ImageView and TextView to it.
private RelativeLayout parentlayout;
TextView notification;
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.deals);
chatHead.setId(imageid);
parentlayout = new RelativeLayout(this);
notification = new TextView(this);
notification.setTextColor(Color.parseColor("#494949"));
notification.setText("1");
notification.setId(nameid);
notification.setTextSize(19);
final RelativeLayout.LayoutParams params_imageview = new RelativeLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
params_imageview.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final RelativeLayout.LayoutParams params_name = new RelativeLayout.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
params_name.addRule(RelativeLayout.ALIGN_RIGHT, imageid);
params_name.addRule(RelativeLayout.ALIGN_TOP, imageid);
parentlayout.addView(chatHead, params_imageview);// adding user image to view
parentlayout.addView(notification, params_name);
Finally make these changes:
mWindowManager.updateViewLayout(parentlayout, params);
and
mWindowManager.addView(parentlayout, params);
PS: Use shapes to style TextView and get exact notification as that of Facebook!! :)

how to set LinearLayout in bottom of FrameLayout programmatically?

I have this code :
FrameLayout game = new FrameLayout(this);
LinearLayout gameWidgets = new LinearLayout (this);
game.addView(gameWidgets);
How to set the LinearLayout in the buttom of the FrameLayout ?
Try this,
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
game.addView(GameWidgets, params);
Use this code:
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL);
game.addView(GameWidgets, p);

Categories

Resources