how to set both horizontal and vertical layout params programmatically - android

In xml file we can do;
android:layout_gravity="center_vertical|right"
I couldn't find how to set those params programmatically. I think something like this is not possible.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.gravity = Gravity.BOTTOM;
params.gravity = Gravity.CENTER;

params.gravity = Gravity.BOTTOM | Gravity.CENTER;

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);

When I use the "addContentView" and " layoutParams.topMargin",the latter is disabled. Why?

My code is as below :
RemindView remindView = new RemindView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.TOP;
layoutParams.topMargin = ViewUtils.getViewHeight(view);
addContentView(remindView, layoutParams);
But the margin attribute i.e layoutParams.topMarginis disabled. I want to know the cause of this problem. Please help.

How to align an ImageButton in Android FrameLayout programmatically

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);

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