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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set the main layout to our xml file with empty layouts setContentView(R.layout.activity_activity_main); //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 mLinearLayout.removeAllViews(); mLinearLayout.addView(mainView); //once the view is ready the layout listener will be called mLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(mainViewLayoutListener); } /*** * 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() { @SuppressWarnings("deprecation") @SuppressLint("NewApi") @Override 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) { obs.removeOnGlobalLayoutListener(this); } else { obs.removeGlobalOnLayoutListener(this); } //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())); } }; }
Code language: Java (java)

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) { super(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 */ @SuppressWarnings("deprecation") @SuppressLint("NewApi") 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.setColor(Color.GREEN); textPaint.setAntiAlias(true); textPaint.setSubpixelText(true); textPaint.setTextSize((float) screenBounds.x / 30f); } @Override public void draw(Canvas canvas) { super.draw(canvas); //fill the entire view with black as a base color canvas.drawColor(Color.BLACK); //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); lineText.reset(); 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); } }
Code language: Java (java)

The main Activity’s XML:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:id="@+id/layoutlinearmaintopcontainer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"> <LinearLayout android:layout_width="fill_parent" android:id="@+id/layoutlinearmain" android:layout_height="fill_parent" android:orientation="horizontal"> <!-- this is where the draw screen will be added programmatically --> </LinearLayout> </LinearLayout>
Code language: plaintext (plaintext)