Note that there are some explanatory texts on larger screens.

plurals
  1. POgetWidth() returns 0 if set by android:layout_width="match_parent"
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;com.antonc.fractal_wallpaper.FractalView android:id="@+id/fractal" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>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():</p> <pre><code>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) { [...] } } </code></pre> <p>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. <a href="https://stackoverflow.com/a/3594216/1204377">this one</a> and they all explain that getWidth() returns the size of an image <em>after</em> it has been drawn, while what I actually need is the width of the area which is available to this view, <em>before</em> I start drawing.</p> <p>Is there a way to retrieve such values for width and height?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload