saurabytes
saurabytes

Reputation: 9

Changing view dimensions programmatically on android

I have two views which overlap each other and one of them acts as a master to other. I am translating all the pinching and panning to scale and adjust the master view. Now I wanna send this info over to the slave(Subclass of android.view.View). Being said that the other view doesn't respond to this dimensions. I have tried following methods

ViewGroup.LayoutParams params = this.getLayoutParams();
params.height = height;
params.width = width;
this.setLayoutParams(params);this.requestLayout();

setMeasuredDimension(width, height);

this.setMinimumHeight(height);
this.setMinimumWidth(width)

None of these seem to work.

Using the onTouchEvent I resize my TextureView and send the same to the other View object. On getting this Info about change in size which is in most cases greater than the parent dimensions(and screen size) the other view should also increase/change its size. The subclassed view doesn't grow to the dimensions supplied to it. Moreover the increase is limited to the parent size(in my case its width: 1080 height: 1776) while it can go above that in the textureView which uses

textureView.setMinimumWidth(newWidth);
textureView.setMinimumHeight(newHeight);

Any ideas as to what maybe going wrong or a trusted way to change layout/dimensions across two views?

Upvotes: 0

Views: 2732

Answers (2)

Sound Conception
Sound Conception

Reputation: 5411

The methods you've listed are all valid ways of setting sizes if used correctly, and in the right situations.

This is difficult to solve without further information.

In what way isn't it working? Is it causing a crash, or are the view sizes just not what you expect?

Where does that snippet sit in your code? I would guess its part of a custom class of your own which extends ViewGroup, but as you're new to stackoverflow we have no idea of your level of programming experience. It might be in completely the wrong place, and this refers to some other object entirely.

One thing I can suggest you check :
You mention your slave is a subclass of android.view.View, but this on its own is not enough to be able to use ViewGroup.LayoutParams. You need to ensure your slave is a direct or indirect subclass of android.view.ViewGroup.

Provide more of your code and info on what you are observing, and we will try and help you further.


Update:
Ok, so it's a TextureView that you are trying to resize.

I'd recommend you check the reported size of your TextureView after you set it's height and width. You could do this with break-points and run a debug, or you could use myTextureView.getWidth() and .getHeight(), and use Log statments to output them to the LogCat.
You can also check the size of the SurfaceTexture associated with your TextureView. Set a SurfaceTextureListener on your TextureView. When this listener is triggered, onSurfaceTextureAvailable() will provide the width and height of the SurfaceTexture.

I suggest this because you may find that you are able to change the size of the TextureView, but it is the content of the the TextureView that is not changing size. In this case you can override TextureView.onSizeChanged(int w, int h, int oldw, int oldh) to define the necessary transform to scale your content between the old and new sizes.

Upvotes: 0

mbonnin
mbonnin

Reputation: 7022

  • setLayoutParams => that would be the canonical way to position a View inside its parent.
  • setMeasuredDimension => this is used from View.onMeasure to tell the layout system what size your particular View wants. Use only if you're implementing your own View subclass.
  • setMinimumWidth/setMinimumHeight => tells the default View implementation what minimum size the View should have. If you implement View.onMeasure yourself, that shouldn't be needed.

All in all, if you stick with the standard Android framework components, setLayoutParams should be the place to look into.

Upvotes: 1

Related Questions