Matt
Matt

Reputation: 2680

Setting the size and position of a Surface View

I'm working on an interface where one half of the screen has edit text boxes for the user to enter data into, and the other half has a canvas that can be drawn to and reads user touches.

Right now I'm extending a surface view and adding that to my layout(Layout is XML) at runtime with View.Addview.

My problem is setting the size and position of the view. I want it to be square and take up about half of the screen (landscape). I want it to be aligned to the right side and either centered vertically, or aligned with the top.

I can set the size by overriding the onMeasure method, but I don't know how to get the display size so I don't know what to set it to.

I don't have any ideas about how to align the view, I've been trying adding it to various layouts without much luck...

Thanks for any help!

Upvotes: 1

Views: 7749

Answers (2)

philippe
philippe

Reputation: 51

but I don't know how to get the display size

Normally you do not have to explicitely layout components. Its the duty of layout manager. You just have to set layout constraints.

Here is how to get the screen size.

yourActivity.getWindowManager().getDefaultDisplay().getWidth();

Upvotes: 1

john w
john w

Reputation: 21

have same question. did you find answer?

I figured it out. Just modify the onlayout(), set something like:

View child=getChildAt(0);
child.layout(100,100,300,300); 

and do not touch onmeasure().

Upvotes: 2

Related Questions