0

I'm trying to draw a Canvas in a View and show this View under some TextViews I defined in my XML file. Everytime I test the App it just doesn't start on the device.

However, the parts themself work: When I change the setContentView(R.layout.activity_main); in the onCreate() to setContetView(new CustomView(this)); it works, but of course without the textView.

My activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/sampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"/>

    <com.example.simon.drawtest.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sampleText"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

My MainActivity.java:

package com.example.simon.drawtest;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
 }
}

My CustomView.java:

package com.example.simon.drawtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class CustomView extends View {

    private Paint paint;

    public CustomView(Context context) {
        super(context);   
        paint = new Paint();
        paint.setColor(Color.GRAY);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);
        canvas.drawCircle(200, 200, 100, paint);
    }
}
Simon
  • 397
  • 1
  • 3
  • 24

1 Answers1

1

show your whole activity_main.xml...

both Views should be wrapped in e.g. FrameLayout

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/sampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"/>
    <com.example.simon.drawtest.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sampleText"
        android:layout_alignParentBottom="true"/>
</FrameLayout>

edit: ok, so you have RelativeLayout.. also note that with Android 5.0 elevation attribute was introduced and you may have some problems with drawing order like HERE

also... your TextView is first in XML so it will be drawn before your custom view (drawing by order if elevation isn't set)

also... android:layout_below="@+id/sampleText" below means under in vertical axis. try to remove this line

edit: user post exception stack trace in comments, so below I'm pasting proper answer for his case (comment copied):

your error stack says

Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]

you have only one constructor with Context only, system needs also one with additional AttributeSet. check out THIS question & answer, apply proper constructors for your CustomView

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • added the rest of it. I had them wrapped in RelativeLayout. Is there a difference (significant to the problem)? – Simon Oct 27 '16 at 21:25
  • Calling `super.onDraw()` will only draw the background and any registered drawables, right? It may be "just" the ordering of the child elements in the layout :) – milosmns Oct 27 '16 at 21:27
  • calling super in `onDraw` in this simple example will surely draw transparent background only (so.. nothing), but when project will grow this super call might do some additional work, especially when user is drawing custom shapes or use some Material-introduced params/atts :) – snachmsm Oct 27 '16 at 21:32
  • Ok i removed the `below`attribute and moved CustomView before TextView. The problem remains. Do I acctualy call an Instance of CustomView? – Simon Oct 27 '16 at 21:35
  • try to set fixed dimens for both your `View`s - e.g. 100dp for `layout_width` and `layout_height`. note that `wrap_content` isn't recognizing custom shapes drawn inside `onDraw` and this `View` will be 0px x 0px size (you should also overrride `onMeasure` method and set dimens in there). it works when you use `setContetView(new CustomView(this));` becaue then your `View` fullfil whole `Activity` – snachmsm Oct 28 '16 at 08:38
  • Thank you so much. I will try this as soon as I'm back from work. Theoretically speaking: The app should still run, shouldn't it? Why is this problem causing it to not run at all? – Simon Oct 28 '16 at 10:31
  • you have to clarify what is going on when you start your app on device.. is it even launching? - really weird if not, because of one-line change... launching and crashing? - show some logcat with exception stack trace. or launching and showing blank "page"? - all my suggestions considers this scenario ;) but in this case its weird that you don't see `TextView` (custom drawing like you do is really tricky and there is a lot of dependeces) – snachmsm Oct 29 '16 at 18:57
  • The Application is simply crashing. The error logcat is displaying is: Binary XML file line #9: Error inflating class com.example.simon.drawtest.CustomView [Gist-Link to complete error-code](https://gist.github.com/anonymous/baecffc681fa555ac9f5718422a7941b) – Simon Nov 01 '16 at 10:05
  • I should add that I changed some lines. So the error is at the line where i say ` – Simon Nov 01 '16 at 10:13
  • your error stack says `Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet]`. you have only one constructor with `Context` only, system needs also one with additional `AttributeSet`. check out [THIS](http://stackoverflow.com/questions/9195713/do-i-need-all-three-constructors-for-an-android-custom-view) question & answer, apply proper constructors for your `CustomView` – snachmsm Nov 01 '16 at 10:35
  • YES!! this was the problem. Thank you so much for your help. Everything else was correct but this. Can you maybe edit your answer and add this to it, so other people will see it and i can mark it as the correct answer. Thanks again! – Simon Nov 01 '16 at 11:08
  • done :) always paste your exception stack trace, as you see it helps a lot with tracking your problem. good luck! – snachmsm Nov 02 '16 at 07:33