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"?>
        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 -->
This entry was posted in Android, Java and tagged , , , , . Bookmark the permalink.