Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving difficulty creating my own custom view
    primarykey
    data
    text
    <p>So I'm trying to create my own view outside of xml layouts and am having difficulty getting it to display anything. I'm sure I'm missing something simple, but cannot see what it might be. Any input is appreciated. Here are my two classes I'm using for testing purposes.</p> <pre><code>public class TestSuite extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Turns off the application title at the top.. requestWindowFeature(Window.FEATURE_NO_TITLE); // Turns off the status bar at the top.. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(new Background(this)); } } </code></pre> <p>and</p> <pre><code>public class Background extends View { public Background(Context context) { super(context); } public Background(Context context, AttributeSet attrs) { super(context, attrs); } public Background(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); Resources res = getResources(); Rect baseRectBounds = new Rect(0,0,200,200); Rect topRectBounds = new Rect(20,20,160,160); Rect bottomRectBounds = new Rect(40,40,120,120); Paint baseColor = new Paint(res.getColor(R.color.blue)); Paint topColor = new Paint(res.getColor(R.color.red)); Paint bottomColor = new Paint(res.getColor(R.color.green)); canvas.drawRect(baseRectBounds, baseColor); canvas.drawRect(topRectBounds, topColor); canvas.drawRect(bottomRectBounds, bottomColor); } } </code></pre> <p><em>edit</em></p> <p>Here's the newest onDraw method I'm working with.</p> <pre><code>@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); Resources res = getResources(); Rect canvasBounds = canvas.getClipBounds(); Log.v("CanvasBounds before", "left - " + canvasBounds.left); Log.v("CanvasBounds before", "top - " + canvasBounds.top); Log.v("CanvasBounds before", "right - " + canvasBounds.right); Log.v("CanvasBounds before", "bottom - " + canvasBounds.bottom); Rect baseRect = new Rect(0,0,200,200); Rect topRect = new Rect(20,20,160,160); Rect botRect = new Rect(40,40,120,120); Paint baseColor = new Paint(res.getColor(R.color.red)); Paint topColor = new Paint(res.getColor(R.color.green)); Paint botColor = new Paint(res.getColor(R.color.blue)); baseColor.setStyle(Style.FILL); topColor.setStyle(Style.FILL); botColor.setStyle(Style.FILL); canvas.drawRect(baseRect, baseColor); canvas.drawRect(topRect, topColor); canvas.drawRect(botRect, botColor); canvasBounds = canvas.getClipBounds(); Log.v("CanvasBounds after", "left - " + canvasBounds.left); Log.v("CanvasBounds after", "top - " + canvasBounds.top); Log.v("CanvasBounds after", "right - " + canvasBounds.right); Log.v("CanvasBounds after", "bottom - " + canvasBounds.bottom); } </code></pre> <p>The results of the Log statements are as follows:</p> <pre><code>CanvasBounds before left - 0 CanvasBounds before top - 0 CanvasBounds before right - 480 CanvasBounds before bottom - 800 CanvasBounds after left - 0 CanvasBounds after top - 0 CanvasBounds after right - 480 CanvasBounds after bottom - 800 </code></pre> <p><em>finished edit</em></p> <p>Thanks to Ted's help, here's my final onDraw method (for others who might need it).</p> <pre><code>@Override public void onDraw(Canvas canvas) { super.onDraw(canvas); Resources res = getResources(); Rect baseRect = new Rect(0,0,200,200); Rect topRect = new Rect(20,20,160,160); Rect botRect = new Rect(40,40,120,120); Paint color = new Paint(); color.setColor(res.getColor(R.color.red)); canvas.drawRect(baseRect, color); color.setColor(res.getColor(R.color.blue)); canvas.drawRect(topRect, color); color.setColor(res.getColor(R.color.green)); canvas.drawRect(botRect, color); } </code></pre>
    singulars
    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