Android: Get the size of a custom view before display.

When developing applications or simple games for Android where you are drawing or writing to a view, an issue arises upon startup of the view. In my case I had a game where I wanted to calculate screen tile size before the game view starts, but with most tutorials I had seen everything happened after the view was fully running. That turned into a chicken and the egg scenario. I needed some key information before I could start the game view, but the default techniques didn’t allow for that.

Here is the code on GitHub for this article.

I was able to get around the issue with a few techniques.

  • Have my main Activity linked with an xml layout file that has two empty nested linear layouts that fill the screen.
  • Make a custom view that houses the main draw code. For this example, all I do is fill the screen with black and display the available screen size.
  • Attach a ViewTreeObserver.OnGlobalLayoutListener in the Activity that will be the boot code upon getting the screen’s view size.

I’m not showing everything here. Look at the project on GitHub for that.

The main activity:

public class ActivityMain extends Activity {

    LinearLayout mLinearLayout;
    ViewMain mainView;

    protected void onCreate(Bundle savedInstanceState) {

        // Set the main layout to our XML file with empty layouts

        // Get a reference to the interior layout we want to draw on
        mLinearLayout = (LinearLayout) findViewById(;

        // Instantiate our custom view
        mainView = new ViewMain(this);

        // Clean up the layout if needed and push our custom view into it

        // Once the view is ready, the layout listener will be called

     * This is used so we can get screen size before everything is finished loading
     * and visible on the screen
    ViewTreeObserver.OnGlobalLayoutListener mainViewLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            ViewTreeObserver obs = mLinearLayout.getViewTreeObserver();

            // Deal with the view tree differently based on Android SDK version
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            } else {

            // Save what will be the full screen size
            // Finish the view startup process now that we know the size
            mainView.initialize(new Point(mLinearLayout.getWidth(), mLinearLayout.getHeight()));

The custom view:

public class ViewMain extends View {

    Paint textPaint = new Paint();
    Rect messageBounds = new Rect();
    Path lineText = new Path();
    String viewWidthMessage = "";
    String viewHeightMessage = "";

    public ViewMain(Context parentActivityContext) {

     * This function is called by the parent activity once screen bounds are calculated
     * This is where all code to initialize something like a game would go
    public void initialize(Point screenBounds) {
        viewWidthMessage = "View Width: " + String.valueOf(screenBounds.x);
        viewHeightMessage = "View Height: " + String.valueOf(screenBounds.y);

        // Make a paint instance that will be used when drawing text
        textPaint.setTextSize((float) screenBounds.x / 30f);

    public void draw(Canvas canvas) {

        // Fill the entire view with black as a base color

        // Draw text on the view
        // Get the size of the first line of text we will display on screen
        textPaint.getTextBounds(viewWidthMessage, 0, viewWidthMessage.length(), messageBounds);
        int xPosition = 1;
        int yPosition = 1;
        int currentLineHeight = yPosition + messageBounds.height() * 2;

        lineText.moveTo(xPosition, currentLineHeight);
        lineText.lineTo(xPosition + messageBounds.width(), currentLineHeight);
        canvas.drawTextOnPath(viewWidthMessage, lineText, 0, 0, textPaint);

        currentLineHeight += messageBounds.height() * 2;
        lineText.moveTo(xPosition, currentLineHeight);
        textPaint.getTextBounds(viewHeightMessage, 0, viewHeightMessage.length(), messageBounds);
        lineText.lineTo(xPosition + messageBounds.width(), currentLineHeight);
        canvas.drawTextOnPath(viewHeightMessage, lineText, 0, 0, textPaint);

The main Activity’s XML:

<?xml version="1.0" encoding="utf-8"?>

        <!-- this is where the draw screen will be added programmatically -->