Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:50:25+00:00 2026-06-18T06:50:25+00:00

I have a class called FractalView that extends ImageView. My goal is to draw

  • 0

I have a class called FractalView that extends ImageView. My goal is to draw a fractal with this class that has the size of the whole screen. It is the only View in my activity’s layout file, other than the top-level LinearLayout:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
    android:background="#FFFFFFFF">

    <com.antonc.fractal_wallpaper.FractalView
        android:id="@+id/fractal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
</LinearLayout>

As you can see, I’m using “match_parent” for both layout_width and layout_height of both the FractalView and the LinearLayout. At this point I assumed that the size of the FractalView is set to a certain value, since it’s matching it’s parent, which is matching it’s parent(which is the whole screen).
Then I try to detect the size that is available to FractalView by using getWidth() and getHeight():

public class FractalView extends ImageView {

private Context mContext;
private Handler mHandler;

private int mScrWidth;
private int mScrHeight;

public FractalView(Context context) {
    super(context);
    mContext = context;
    init();
}

private void init() {

    mScrWidth = getWidth();
    mScrHeight = getHeight();
    // Breakpoint
}

@Override
protected void onDraw(Canvas canvas) {
    [...]
}

}

But when my code reaches the breakpoint, the debugger shows that mScrWidth and mScrHeight are zero. I’ve tried using getMeasuredWidth() and getMaxWidth(), but they also return incorrect values. I’ve read several answers to similar questions, i.e. this one and they all explain that getWidth() returns the size of an image after it has been drawn, while what I actually need is the width of the area which is available to this view, before I start drawing.

Is there a way to retrieve such values for width and height?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:50:26+00:00Added an answer on June 18, 2026 at 6:50 am

    A view’s size isn’t available until after onMeasure(), particularly if its set to wrap_content, or fill_parent.

    You should either access the size in or after onMeasure() in the View code, or in a a Layout Tree Observer:

    LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
    ViewTreeObserver vto = layout.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            int width  = layout.getMeasuredWidth();
            int height = layout.getMeasuredHeight(); 
    
        } 
    });
    

    You will also need to add android:id="@+id/mylayout" to your LinearLayout for the second one to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Flamethrower which naturally has its own ammunition that is
I have a class called GameState in its own file and that class has
I have a class called photo. This class has 4 fields, one of while
I have a class called Lezione that has an attribute time of type Time
I have a class called Foo that has a function that looks like the
I have a class called Plato that extends Producto witch contains de atribute precio.
I have a class called ProjectTaskBudget that has a property of type IBudgetableIncomeStream ,
I have a class called: ComplexNumber and I have a string that I need
I have a class called Property which has nothing but get -methods. All the
I have a class called Property that I only need to display a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.