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!! :)
Related
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);
I'm trying to show a TextView in a FrameLayout when the marker is visible.
Like in the code below:
FrameLayout frameLay = new FrameLayout(context);
FrameLayout.LayoutParams layoutParamsFrame = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
frameLay.setLayoutParams(layoutParamsFrame);
TextView theText = new TextView(context);
theText.setText("text_test");
theText.setTextColor(Color.WHITE);
theText.setTypeface(Typeface.DEFAULT_BOLD);
LayoutParams layoutParamsText = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
theText.setLayoutParams(layoutParamsText);
theText.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
first = true;
frameLay.addView(theText);
This does not work for me. The marker is visible and I execute this code, but nothing happens.
How can I fix this?
you can read this post it is explained how to add a TextView to a FrameLayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
((FrameLayout) findViewById(R.id.mainLayout)).addView(mEditText, params);
So basically you have to add to addView two parameters
so becomes
frameLay.addView(theText,layoutParamsText );
My app's layout is FrameLayout. I want to align an ImageButton to right top of the framelayout. I tried below code not worked.
mImageButton = new ImageButton(mContext);
LayoutParams ll = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT );
ll.gravity = Gravity.RIGHT;
mImageButton.setLayoutParams(ll);
mImageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_moreoverflow_normal_holo_light));
this.addView(mImageButton);
expected output
I think you should change the construction code of LayoutParams to:
LayoutParams ll = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.gravity = Gravity.TOP | Gravity.RIGHT;
In your code, you set the layout_width and layout_height value to MATCH_PARENT , so your ImageView will fill all your parent FrameLayout.
Try this,
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
mImageButton.setLayoutParams(params);
I think you need to provide height width in layout params. Because with match_parent it will take width and height of parent or you can use WRAP_CONTENT.
mImageButton = new ImageButton(mContext);
LayoutParams ll = new LayoutParams(width,height);
ll.gravity = Gravity.RIGHT | Gravity.TOP;
mImageButton.setLayoutParams(ll);
mImageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_moreoverflow_normal_holo_light));
this.addView(mImageButton);
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
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);