Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing on RelativeLayout's Canvas behind all other views?
    primarykey
    data
    text
    <p>I'm using <code>RelativeLayout</code> to absolutely position some standard views (like <code>TextView</code>).</p> <p>What I'd like to do is to draw a custom line on this <code>RelativeLayout</code>'s <code>Canvas</code> using <code>Canvas.drawLine</code> that is drawn <em>behind</em> all its other subviews.</p> <p>These other subviews are added with explicitely defining <code>RelativeLayout.LayoutParams</code>, but I'd like to leave the decision of <em>where</em> to paint itself to my custom line.</p> <p>I tried wrapping this line in a CustomView with overloaded <code>View.onDraw(Canvas canvas)</code> method and simply adding the view <em>without</em> specifying any <code>LayoutParams</code>, so:</p> <pre><code>public class CustomView extends View { public CustomView(Context context, int x0, int y0, int x1, int y1) { super(context); setClickable(false); setBackgroundColor(Color.TRANSPARENT); } public void onDraw(Canvas canvas) { Log.i("myapp", "i'm not called! :(") Paint p = new Paint(); p.setColor(Color.BLACK); canvas.drawLine(x0, y0, x1, y1, p); } } </code></pre> <p>And usage:</p> <pre><code>CustomView v = new CustomView(MyActivity.this, 0, 0, 100, 100); relativeLayout.addView(v); </code></pre> <p>... but this <code>onDraw</code> method is never called.</p> <p>Is there a way to make this work?</p> <hr> <p><strong>Edit</strong>: works if I substitute:</p> <pre><code>relativeLayout.addView(v) </code></pre> <p>with</p> <pre><code>relativeLayout.addView(v, new RelativeLayout.LayoutParams(SOME_WIDTH, SOME_HEIGHT)); </code></pre> <p>The point is, I know neither <code>SOME_WIDTH</code>, nor <code>SOME-HEIGHT</code> at that point.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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