Android: Set match_parent in custom view programmatically

I have implemented a custom view in which I can draw. I want to set it to MATCH_PARENT so that it fills the screen regardless of the screen orientation. When I change orientation to landscape, it only fills 50% of the width.

I changed onMeasure () but there was no effect:

public class DrawScreen extends View{

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);   
        setMeasuredDimension(screenWidth, screenHeight);
        setMeasuredDimension(screenHeight, screenWidth);


public class MyService extends Service{

windowManager.addView(toolbox, params);



source to share

1 answer

You can try this:

final DrawScreen view = new DrawScreen(...);
final LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);




All Articles