mayhem
mayhem

Reputation: 91

Getting the width/height of a layout in Android

I'm wondering how to measure the dimensions of a view. In my case it is aan Absolute Layout. I've read the answers concerning those questions but I still don't get it.

This is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);      

    AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);       

    drawOval(); 

}
public void drawOval(){ //, int screenWidth, int screenHeight){ 
    AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);     

    int screenWidth = layoutbase.getWidth();
    int screenHeight = layoutbase.getHeight();
    Log.i("MyActivity", "screenWidth: " + screenWidth + ", screenHeight: " +screenHeight);

    Coordinates c = new Coordinates(BUTTONSIZE,screenWidth,screenHeight);

    ...some code ...

    ((ViewGroup) layoutbase ).addView(mybutton, new AbsoluteLayout.LayoutParams(BUTTONSIZE, BUTTONSIZE, c.mX, c.mY));


    mybutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showText(mybutton);
        }
    });
}

public void showText(View button){      

    int x = findViewById(LAYOUT).getWidth();
    int y = findViewById(LAYOUT).getHeight(); 

    Toast message = Toast.makeText(this, "x: " + x , Toast.LENGTH_SHORT);       
    message.show();     


}

The getWidth() command works great in showText() but it does not in drawOval(). I know it looks a bit different there but I also used the int x = findViewById(LAYOUT).getWidth(); version in drawOval(), and x/y are always 0. I don't really understand why there seems to be no width/height at that earlier point. Even if I actually draw a Button on the Absolute Layout, getWidth() returns 0. Oviously I want to measure the sizes in drawOval().

Upvotes: 9

Views: 45330

Answers (6)

Bebin T.N
Bebin T.N

Reputation: 2649

I think will help you.

   LinearLayout headerLayout = (LinearLayout)findviewbyid(R.id.headerLayout);
    ViewTreeObserver observer = headerLayout .getViewTreeObserver();
            observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            int headerLayoutHeight= headerLayout.getHeight();
        int headerLayoutWidth = headerLayout.getWidth();
        headerLayout .getViewTreeObserver().removeGlobalOnLayoutListener(
                this);
    }
});


}  

Upvotes: 11

Joker
Joker

Reputation: 755

This specially deal with Dimensions so

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight();

This may help you in managing dimensions.

Note: This returns the display dimensions in pixels - as expected. But the getWidth() and getHeight() methods are deprecated. Instead you can use:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

as also Martin Koubek suggested.

Upvotes: 3

Martin Koubek
Martin Koubek

Reputation: 453

This give you screen resolution:

 WindowManager wm = (WindowManager)context.getSystemService(context.WINDOW_SERVICE);
 Display display = wm.getDefaultDisplay();
 Point outSize = new Point();
 display.getSize(outSize);

Upvotes: 2

Alex Lockwood
Alex Lockwood

Reputation: 83303

kabuko's answer is correct, but could be a little more clear, so let me clarify.

getWidth() and getHeight() are (correctly) giving you 0 because they have not been drawn in the layout when you call them. try calling the two methods on the button after addView() (after the view has been drawn and is present in the layout) and see if that gives you the expected result.

See this post for more information.

Upvotes: 1

Alex Lockwood
Alex Lockwood

Reputation: 83303

If your goal is to simply draw an oval on the screen, then consider creating your own custom View rather than messing around with AbsoluteLayout. Your custom View must override onDraw(android.graphics.Canvas), which will be called when the view should render its content.

Here is some extremely simple sample code that might help get you started:

public class MainActivity extends Activity {

    private final Paint mPaint = new Paint();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    // create a nested custom view class that can draw an oval. if the
    // "SampleView" is not specific to the Activity, put the class in 
    // a new file called "SampleView.java" and make the class public 
    // and non-static so that other Activities can use it. 
    private static class SampleView extends View {
        public SampleView(Context context) {
            super(context);
            setFocusable(true);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.CYAN);

            // smoothen edges
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE); 
            mPaint.setStrokeWidth(4.5f);

            // set alpha value (opacity)
            mPaint.setAlpha(0x80);

            // draw oval on canvas
            canvas.drawOval(new RectF(50, 50, 20, 40), mPaint);
        }
    }
}

Upvotes: 2

kabuko
kabuko

Reputation: 36302

getWidth() is giving you 0 because onCreate is called before layout actually happens. Due to views being able to have dynamic positions and sizes based on attributes or other elements (fill_parent for example) there's not a fixed size for any given view or layout. At runtime there is a point in time (actually it can happen repeatedly depending on many factors) where everything is actually measured and laid out. If you really need the height and width, you'll have to get them later as you've discovered.

Upvotes: 7

Related Questions