Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Set match_parent in custom view programmatically

I implemented a custom view where I can paint. I want to set it to MATCH_PARENT, so that it fills the screen independent of the screen orientation. When I change orientation to landscape it fills just 50% of width.

I changed onMeasure() but there was no effect:

public class DrawScreen extends View{

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);   
    if(context.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
        setMeasuredDimension(screenWidth, screenHeight);
    }else{
        setMeasuredDimension(screenHeight, screenWidth);
    }

}
}


public class MyService extends Service{

...
windowManager.addView(toolbox, params);
}
like image 702
Kewitschka Avatar asked Apr 27 '15 20:04

Kewitschka


1 Answers

You could try the following:

final DrawScreen view = new DrawScreen(...);
final LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
view.setLayoutParams(lp);
like image 111
patrick.elmquist Avatar answered Oct 12 '22 00:10

patrick.elmquist