Android: Relative layout, change X coord - android

I have an Activity with relative layout on whole screen. In left top corner I have button. And on button click I want move layout from 0 X coord to 100 X coord. I tried something like this in onclick method:
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
param.leftMargin = 100;
meRelativeLayout.setLayoutParams(param);
But this thing just made my relative layout width smaller, its change left margine and thats it, but I don't want to change layout width, just want to change X coord, is there any way to do that?

To me your code seems to be perfect, only the matter is of FILL_PARENT. As you used FILL_PARENT then the system will assign the space left in the display(less margin) thats why you layout became small, So Try this.
int width= mActivity.getWindowManager().getDefaultDisplay().getWidth();
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams( width, LayoutParams.FILL_PARENT);
param.leftMargin = 100;
meRelativeLayout.setLayoutParams(param);

Related

Programmatically change height in constraint layout?

I have a simple layout: a fixed height TextView with three ImageView objects anchored to the bottom of the view.
How can I programmatically change the height of each of the ImageViews so they vary within some range (10dp, 16dp below the TextView)? Concretely, my activity gets a list of percentages (between 0 and 1) representing how much of that vertical space each of those "bars" should take up.
I've tried the following without success:
// No visible difference
imgView.setMinHeight(newHeight);
// No change to height, but horizontal constrained was messed up
imgView.setLayoutParams(new LayoutParam(imgView.getWidth(), newHeight);
If by "height" you mean the absolute top-to-bottom dimension of the ImageView, you can do the following to double the height of an ImageView. You can set the height to the value that you want.
ImageView iv = (ImageView) findViewById(R.id.imageView);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) iv.getLayoutParams();
lp.height = lp.height * 2;
iv.setLayoutParams(lp);
See ConstraintLayout.LayoutParams.
But, if by "height" you mean the vertical position of the ImageView, you can do the following to change the ImageView's vertical bias (assuming that you are using ConstraintLaytout):
ConstraintSet cs = new ConstraintSet();
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);
cs.clone(layout);
cs.setVerticalBias(R.id.imageView, 0.25f);
cs.applyTo(layout);
See setVerticalBias.
There are other ways to move an ImageView about the layout, but which you use would depend on how your layout is set up.

Change edittext with equally along left and right when setting width

I have following setup.
There is an edittext with controllers on left and right for resizing.
When user resizes by touching and moving the left controller, I am calculating the distance between moved x-coordinate and the mid-point of right side of edittext.
The idea is that the right side has to remain static, where as on resizing from left, the left bound has to increase of decrease like wise. I am assigning new width and center to edittext on resize from left as follows:
ivLeftControl.setPivotX(traversedPoint.x);
editTextViewNew.setGravity(Gravity.CENTER);
editTextViewNew.setWidth((int) (traversedWidth-ivLeftControl.getWidth()*2));
editTextViewNew.setPivotX(centerX - (traversedWidth - originalWidth)/2);
But the resize is happening only along the right side of edittext, instead of happening along left side. Even when I dont change the pivot, the resize happens along right side only.
Any suggestion on how this features could be addressed the best?! Keeping in mind, the entire setup of controllers and edittext is enveloped using a parent layout.
Solution - MarginLayoutParams
you need to create MarginLayoutParams and then set height , width , margins in it and set to your view.
MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams();
layoutParams.width = mPixelSize;
layoutParams.height = mPixelSize;
layoutParams.setMargins(mLeftMargin, mTopMargin, 0, 0);
requestLayout();

Android change size programmatically

I need to change the size of the buttons programmatically.
I read some answers and I do the next to change the button size.
button.setLayoutParams(new RelativeLayout.LayoutParams(150, 50));
This code change the size correctly but the problem is that the button not stay in the position I want, it moves the button to the top left corner.
I need to change the button size but I don't want to change de position.
How can I fix this?
You have to use the existing RelativeLayout Rules as well,i.e whether the item is center aligned,align parent left etc. Or you can make use of the existing Params and modify the width and height as follows the RelativeLayout rules will not be modified.
RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.width=150;
layoutParams.height=50;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
params.width = 500;
params.height = 500;
button.setLayoutParams(params);

How to modify a relative layout to fit a specific left, right, top, bottom position on a screen

My relative layout is named "highlight".
public static void selectText(float left, float right, float top, float bottom) {
highlight.getLayoutParams().width =(int) (right-left);
highlight.getLayoutParams().height=(int) (bottom - top);
highlight.setX(left);
highlight.setY(top);
}
This works great for highlighting text as far as setting the top left corner of the highlight box. But, the box expands all the way to the bottom right corner of the screen, no matter how small I make the .width and .height values.
You set your width and height as wrap_content. Your layout will have the size of it's content.
Instead of:
highlight.getLayoutParams().width =(int) (right-left);
highlight.getLayoutParams().height=(int) (bottom - top);
Try:
highlight.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Check this link:
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
I don't know why, but the simple 'get layout params' wasn't working. I needed to make a new layout params entirely.
int width = (int) (right-left);
int height = (int) (bottom - top);
RelativeLayout.LayoutParams rlMainlayoutParams = new RelativeLayout.LayoutParams((int) (right-left), (int) (bottom - top));
highlight.setLayoutParams(rlMainlayoutParams);
highlight.setX(left);
highlight.setY(top);
The above answer doesn't answer my question, perhaps because I was not clear that this relative layout doesn't hold any text, it only highlights certain text already present on the screen (in a PDF document, where I cannot necessarily just extract the text to a PDF document).
After you've modified a view's layout params, call requestLayout() on the view for the changes to take effect.
(Calling setLayoutParams() also implicity calls requestLayout().)

Using RelativeLayout dynamically and setting margins in px, dp, inch, mm

My application is just a modified ImageViewer with the options of zooming and dragging.
Containing that modified Imageviewer there is a RelativeLayout (that I want to use as an AbsoluteLayout).
Then, new elements can be added to the layout to situate them in certain points of the image. The position (x, y) in pixels of the elements is in a database so I guess there is no other way that adding them programmatically, and not with XML.
Then the position of those elements is updated every time that a dragging or zooming action is performed.
The problem is that somehow the pixels of the image are not matching with the pixels in the RelativeLayout! So the items are "more or less" situated but not in the proper place (the further from (0,0) the bigger is the error).
Things I've tried already:
Try different conversions because maybe the problem is related to the difference of densities between the Bitmap and the Layout:
Resources r = getResources();
float x = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics());
Try to use different methods in the layout to set the margins:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) newPosition.x;
params.topMargin = (int) newPosition.y;
element.setLayoutParams(params);
I also wanted to use the options available for the RelativeLayout in XML but I really cant find it programmatically but I can find the way to specify the dimension, I mean:
android:layout_marginLeft ="50px" // or "50mm", or dp, or inches...
You can set the height, width and the margin by below code:
//headView
RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)headView.getLayoutParams();
head_params.setMargins(80, 0, 0, 0); //substitute parameters for left, top, right, bottom
head_params.height = 60; head_params.width = 200;
headView.setLayoutParams(head_params);
Where headView is the view of your app.
Try this: for Relative Layout you can set the margins like this: params.setMargins(left, top, right, bottom);
I've found the answer, the problem was in fact with the density, so I made:
DisplayMetrics metrics = getResources().getDisplayMetrics();
float density = metrics.density;
//And then I add a new element like: realX*density, realY*density
I don't understand so much what it is doing but it works... Actually what I don't understand the value metrics.density
Any explanation is welcome.

Categories

Resources